How to move a file from one folder to another using Java

This is very simple yet tricky. Java has several ways to do it. Below is the easiest one to remember.

import java.io.*;
public class MoveFile
{
    public static void main(String[] args)
    {
        File file = new File("C:\\tobo\\input\\file1.zip");
          
        // The trick is to move the file via renaming it to other location
        if(file.renameTo
           (new File("C:\\tobo\\output\\file2.zip")))
        {
            System.out.println("File has been moved successfully");
        }
        else
        {
            System.out.println("File could not be moved to new location");
        }
    }
}

This is a very simple code. But extensively used.

Happy Coding.

🙂

4 thoughts on “How to move a file from one folder to another using Java

  1. Hmm is anyone else having problems with the
    pictures on this blog loading? I’m trying to figure out if its a problem on my end or if it’s
    the blog. Any suggestions would be greatly appreciated.

  2. An outstanding share! I’ve just forwarded this onto
    a coworker who has been doing a little homework on this.
    And he in fact ordered me lunch due to the fact that I discovered it for him…
    lol. So allow me to reword this…. Thank YOU for the meal!!
    But yeah, thanx for spending some time to discuss this matter here on your site.

  3. Excellent article. Keep writing such kind of info on your site.
    Im really impressed by it.
    Hey there, You have done an incredible job. I’ll certainly digg it and
    in my opinion recommend to my friends. I’m confident they will be benefited from this web site.

  4. Hello! I’ve been reading your site for a long time now and finally got the
    bravery to go ahead and give you a shout out from Austin Tx!
    Just wanted to tell you keep up the good work!

Leave a Reply to bit.ly Cancel reply

Your email address will not be published.

Verified by MonsterInsights