Pages

Moving a directory with its content in java

Sometimes we want to move a folder with its content from one location to another location but there is no native method in java(upto java7) to move a directory with its content. We can only move an empty directory

To move the directory with its content use the apache common io.

The code snippest is given below-


public class FileMove {
       private String destination;

    public void moveFile(File dir) {


    File destDir = new File(destination);

    try {

        FileUtils.moveDirectoryToDirectory(dir, destDir, false);


    } catch (IOException e) {

        e.printStackTrace();
    }
    }