Edgblog

February 20, 2010

How to copy a file in Java

Filed under: java — edgblog @ 5:37 pm

Even the most basic of task – like copying a file – can be done in several different ways in Java…
a testament to the richness of the platform (or its complexity !).

Four possible ways to copy a file below, all error handling left aside.

1- the complicated way, manipulating io streams.

   import java.io.FileInputStream;
   import java.io.FileOutputStream;
   import java.io.InputStream;
   import java.io.OutputStream;
   ....

   String orig ="file.xml";
   String dest = "file.xml.bak";
   InputStream in = new FileInputStream(orig);
   OutputStream out = new FileOutputStream(dest);
   byte[] buf = new byte[1024];
   int len;
   while ((len = in.read(buf)) > 0) {
      out.write(buf, 0, len);
   }
   in.close();
   out.close(); 

2- The ugly , non-portable way, using the operating system environment:


   Runtime.getRuntime().exec("OS dependent file copy command here"); 

3- The opensource way, using the Apache Commons library.


   import java.file.io;
   import org.apache.commons.io.FileUtils;
   ....
   String orig ="file.xml";
   String dest = "file.xml.bak";
   File fOrig = new File(orig);
   File fDest = new File(dest);
   FileUtils.copyFile(fOrig, fDest);

4- The novel way- using Java 7 and it’s revamped I/O api.

   
    import java.io.File;
    import java.nio.file.Path;
    ...
    String orig ="file.xml";
    String dest = "file.xml.bak";
    File f = new File (orig);
    Path p = f.toPath();
    p.copyTo(new File (dest).toPath(), REPLACE_EXISTING, COPY_ATTRIBUTES);

Advertisement

7 Comments »

  1. Hi,

    That’s interesting, thanks :)

    But i think that you can also consider the java.nio way. It is something like that :

    String orig =”file.xml”;
    String dest = “file.xml.bak”;

    FileChannel in = new FileInputStream(orig).getChannel();
    FileChannel out = new FileOutputStream(dest).getChannel();

    in.transferTo(0, in.size(), out);

    in.close();
    out.close();

    It’s normally faster and really easier :)

    Comment by Baptiste Wicht — February 20, 2010 @ 10:09 pm | Reply

  2. I think if you’re going to do this in any kind of serious application, 3 and 4 would be the best choices. These are built on preexisting libraries that are well tested. For 1 and 2, there are many many edge cases that you might not be prepared to handle.

    Comment by luckytoilet — February 21, 2010 @ 5:29 am | Reply

  3. Why not to use apache commons FileUtils.copyFile(File src,File dest) ?

    Comment by alberlau — February 22, 2010 @ 11:05 am | Reply

    • that would be section 3 in the post…

      Comment by edgblog — February 22, 2010 @ 8:01 pm | Reply

  4. I wouldn’t dare to say this is a testament to the richness of the platform :-)

    It just shows how bad the current File/IO API is, and thankfully they are going to change this in Java 7 with NIO2. Everytime I have to do something this easy and I need to write Input/OutputStreams it shows me that the File API was written in a hurry and they didn’t spend enough time thinking about the API the first time. The original authors even agree on this, together with the Date/Time API it was hacked together rather quickly so they could release it.

    Comment by Roy van Rijn — February 22, 2010 @ 12:22 pm | Reply

  5. Ok, I am new to java IO and my assignment is to create a file with only a single number entered on each line of this file and I am supposed to enter teh same number twice. THEN, I have to create a new file that copies the numbers, BUT only once, so it’s like copying the file, but modifying its contents to store only each unique number of the original file. Any ideas, I like the last approach, BUT I don’t understand the last statment:

    p.copyTo(new File (dest).toPath(), REPLACE_EXISTING, COPY_ATTRIBUTES);

    what is REPLACE_EXISTING and COPY_ATTRIBUTES??? Please help

    Comment by Person — March 26, 2010 @ 4:22 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.