24 Sept 2012

Moving Files from one Folder to other folder in java

/* how to move a file from one folder to another in java, Moving Files from one Folder to other folder in java, copy files from one folder to another folder in java*/

import java.io.File;

public class Test 
   {
    public static void main(String[] args) 
    {
        
java.io.File file = new java.io.File("C:\\Test1");

        java.io.File dir = new java.io.File("C:\\Test2");
        java.io.File[] listFiles = file.listFiles();
        for (int i = 0; i < listFiles.length; i++) 
         {
            boolean success = listFiles[i].renameTo(new       

            java.io.File(dir,listFiles[i].getName()));
            if (success) 
           {
                System.out.print("Moved");
            }
              else{
                System.out.print("not Moved");
                    }
        }

    }
}

or

Use org.apache.commons.io.FileUtils class

moveDirectory(
java.io.File srcDir, java.io.File destDir) we can move whole directory

0 comments:

Post a Comment