1

Is this an efficient way to copy all files with in a directory, including child directories? Is there a chance of infinite recursion? Is there anything I should change? I know it works, but I think there should be an easier way to do this.

private void copy(File file, String path) {
        String fileName = file.getPath();
        System.out.println(fileName);
        fileName = fileName.substring(fileName.lastIndexOf("\\"));
        if (path == null)
            path = Storage.getStorageDirectoryPath();
        File toWrite = new File(path + File.separator + fileName);
        if (file.isDirectory()) {
            toWrite.mkdir();
            File inDirectory[] = file.listFiles();
            for (File f : inDirectory)
                copy(f, toWrite.getPath());
        } else {
            try {
                InputStream inStream = new FileInputStream(file);
                OutputStream outStream = new FileOutputStream(toWrite);

                byte buffer[] = new byte[1024];
                int length = 0;
                while ((length = inStream.read(buffer)) > 0) {
                    outStream.write(buffer, 0, length);
                }

                inStream.close();
                outStream.close();
            } catch (IOException e) {
                e.printStackTrace(); 
            }

        }
    }

Thanks

TeJanca
  • 11
  • 1
  • I think it is efficient. – kosa Dec 29 '11 at 02:57
  • What version of Java are you using? Java 7 offers the `Files` class for this purpose. – Bernard Dec 29 '11 at 02:58
  • 3
    If you encounter a `symlink` from a folder to a previous folder, I think you'll encounter an infinite loop. – kba Dec 29 '11 at 03:01
  • After the briefest of reviews, it appears to be a standard recursive file copy algorithm. The only issue would be whether you could possibly end up following a symbolic link that was circular. I don't recall how Java handles symbolic links in their `File` interface. – Hot Licks Dec 29 '11 at 03:02
  • It can be done more quickly by spawning off threads to do the actual file copy. – Hot Licks Dec 29 '11 at 03:03
  • This is to be distributed so I thought I should avoid all Java 7 implementations, for not everyone has Java 7 yet, sadly. Also, I do not know how Java handles shortcuts either. I will Google this conundrum...thank you for your feed back, everyone, it is appreciated. – TeJanca Dec 29 '11 at 03:06
  • If I were to spawn threads, wouldn't there be a possibility of a folder not being created before the file is copied to it? – TeJanca Dec 29 '11 at 03:09
  • 2
    I believe if you use [`getCanonicalPath`](http://docs.oracle.com/javase/6/docs/api/java/io/File.html#getCanonicalPath%28%29) instead of `getPath` you won't have to worry about symlinks because they will be resolved for you. – Paul Dec 29 '11 at 03:22
  • possible duplicate - http://stackoverflow.com/questions/1146153/copying-files-from-one-directory-to-another-in-java – mre Dec 29 '11 at 03:23
  • Also, perhaps you can get the OS to do the work for you. Take a look at this tutorial on using [`Process` and `ProcessBuilder`](http://www.devdaily.com/java/java-exec-processbuilder-process-1). – Paul Dec 29 '11 at 03:26
  • 2
    Why don't you use Apache Common's FileUtils? – st0le Dec 29 '11 at 03:30
  • Not sure if I need to start a new question, but if I wanted to track the speed of this in kb/s how could I do that? Again, I did not feel like opening a new question for something that can be solved in this way, if I need to I will. Thanks. Also I do not want to use other libraries that may or may not bloat the distributed jar. – TeJanca Dec 29 '11 at 04:04

2 Answers2

1

Looks pretty good as the comments indicate. You might want to look into the new Java 7 API's (the new NIO). There's a tutorial here, it looks like there are even options to avoid following links.

If you can't use Java 7, old NIO has channels that you can open after opening the file the old way. They include methods transferFrom and transferTo, which might be able to do it more efficiently than you could in Java.

BillRobertson42
  • 12,602
  • 4
  • 40
  • 57
0

Why reinvent the wheel? take a look at the methods in Apache Common's FileUtils, in particular copyDirectory.

Óscar López
  • 232,561
  • 37
  • 312
  • 386