-3

building a java module in Android Studio according to this link the code below is not compiling, I tried to create a new FileUtil object in order to call the copyDirectory function on it for no avail.

public class MyClass {
public static void main(String[] args) throws IOException {
    String old_name = "Six";
    String new_name = "Seven";

    String old_file_name = "C:\\Users\\Jack\\AndroidStudioProjects\\" + old_name;
    String new_file_name = "C:\\Users\\Jack\\AndroidStudioProjects\\" + new_name;

    File srcDir = new File(old_file_name);
    File destDir = new File(new_file_name);

    copyDirectory(srcDir, destDir);
}

}

Community
  • 1
  • 1
samjesse
  • 378
  • 4
  • 14

1 Answers1

1

You probably need these functions

public static void copyFileOrDirectory(String srcDir, String dstDir) {

    try {
        File src = new File(srcDir);
        File dst = new File(dstDir, src.getName());

        if (src.isDirectory()) {

            String files[] = src.list();
            int filesLength = files.length;
            for (int i = 0; i < filesLength; i++) {
                String src1 = (new File(src, files[i]).getPath());
                String dst1 = dst.getPath();
                copyFileOrDirectory(src1, dst1);

            }
        } else {
            copyFile(src, dst);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.getParentFile().exists())
        destFile.getParentFile().mkdirs();

    if (!destFile.exists()) {
        destFile.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;

    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59