Tuesday, October 28, 2014

Java: Copy a directory recursively to a new location

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class DirectoryCopy {
      public static void main(String[] args) {
            try {
                  DirectoryCopy runner = new DirectoryCopy();
                  runner.copyFile("R:\\FileDepot", "C:\\MyFiles\\ProjectDesign");
            }catch(IOException e) {
                  e.printStackTrace();
            }
      }

      public void copyFile(String sourceDir, String desDir) throws IOException {
        File file = new File(sourceDir);
        File file2 = new File(desDir);
        if (file.isDirectory()) {
            if (!file2.exists()) {
                file2.mkdir();
            }
            String[] allFiles = file.list();
            for (String f : allFiles) {
                copyFile(sourceDir+"\\"+f, desDir+"\\"+f);
            }
        } else {
            if (!file2.exists()) {
                file2.createNewFile();
            }
            Files.copy(file.toPath(), file2.toPath(), StandardCopyOption.REPLACE_EXISTING);
        }

    }
}

--------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment