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);
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
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.
Why not to use apache commons FileUtils.copyFile(File src,File dest) ?
that would be section 3 in the post…
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.
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
it’s all explained here: http://java.sun.com/javase/7/docs/api/java/nio/file/StandardCopyOption.html
Pingback: How to copy a file in Java « Giovanni’s Blog
Pingback: Make a copy of a file inside a folder of the current path - Bizzteams