2

I am making an application that includes file copying, but when I go through a large directory (1000+) files and copy them to another folder, it uses 290+ MB of RAM.

So, is there any way to change the File of FileOutputStream without creating a new instance of the FileOutoutStream class?

EDIT:

Here is my Java 7 API version.

Path source = FileSystems.getDefault().getPath(Drive.getAbsolutePath(), files[i].getName());
        Path destination = FileSystems.getDefault().getPath(Save);
        try {
        Files.copy(source, destination);
        } catch (FileAlreadyExistsException e) {
            File file = new File(Save + files[i]);
            file.delete();
        }

Keep in mind, that this is in a for loop that is being tested on 1000+ file counts. With the current method I am using 270+ MB of RAM

Raedwald
  • 46,613
  • 43
  • 151
  • 237
zfollette
  • 475
  • 4
  • 15
  • 2
    Are you closing your streams? – Andy Thomas May 31 '13 at 20:17
  • Which version of Java? The ideal answer depends on it. Java 7 has a new file API, which is miles better than what was available prior to this. – fge May 31 '13 at 20:21
  • Mine is 7u21. But using 7u17 is an option – zfollette May 31 '13 at 20:23
  • 1
    OK, since this is 7, go see `Files`. See answer from @AndyThomas-Cramer for the link. This class has a convenient `.copy()` method... – fge May 31 '13 at 20:28
  • I have implemented that method, it has only taken about 12MB off of my RAM usage... Any help here? I set all of my variables to null after I am done so the get picked off by GC... I dont know why it is taking up so much RAM – zfollette May 31 '13 at 20:38
  • If you are giving the VM that much RAM it will use it. I'd bet that it will happily run in much less, but doesn't need to GC. You could try expliciitly calling System.gc() after each copy to see what is really being held onto. – Duncan McGregor Jun 17 '13 at 21:42

3 Answers3

7

No, you can't redirect a FileOutputStream to a different file.

If you're using Java 7, you can use the new Files class to copy files. The Files.copy() methods can do most of the work for you.

Otherwise, verify that you're closing your streams. Prior to Java 7's try-with-resources, it could look something like this:

FileOutputStream out = null;
try {
    // Create the output stream
    // Copy the file
} catch (IOException e) {
    // Do something
} finally {
    if ( null != out ) {
       try { out.close(); } catch ( IOException ) { }
    }
}
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • +1, since the OP says he's using Java 7! You could then mention `Files.copy()` – fge May 31 '13 at 20:27
  • You would also need to wrap the `close()` call in a try-catch block to be safe. This leads to Java 7's suppressed exceptions. – Eric Jablow May 31 '13 at 20:28
  • @fge - I had alluded to the Files class, but you're right that mentioning Files.copy() explicitly would be more clear. Added explicit mention of Files.copy() . – Andy Thomas May 31 '13 at 20:31
  • I used to use commons-io's `IOUtils.closeQuietly(...)`. Java 7's technique is much clearer. – Eric Jablow May 31 '13 at 20:33
  • I have commented on the first answer, explaining my further problems with the Java 7 API – zfollette May 31 '13 at 20:42
  • Another option to try would be to handcopy the files with one buffer array (or a small pool of them) for all the files you're copying, rather than creating a new buffer for every file. Also, are you running out of memory, or just consuming lots of the heap? Try -XX:+PrintGCTimeStamps tp see when collection happens. – Andy Thomas May 31 '13 at 20:59
1

Have a look at this question: Standard concise way to copy a file in Java?

specifically

..., Apache Commons IO is the way to go, specifically FileUtils.copyFile(); it handles all the heavy lifting for you.

Community
  • 1
  • 1
ljgw
  • 2,751
  • 1
  • 20
  • 39
0

How about some nio2 from Java 7?

Path source = // ...
Path target = // ...
Files.copy(source, target);    

See javadoc of Files.copy(...) for details. See also the optional parameter CopyOption.

matsev
  • 32,104
  • 16
  • 121
  • 156