Wednesday, February 24, 2010 0 comments

Copying files in Java using NIO

Read an article on Java IO and found out that rather than using the normal stream reader classes in Java we can use the Java NIO api which does native I/O operations rather than going throught he java memory model which significatly improves I/O calls performance. Following is an example i got from that article showing how you can do a simply file copy ;

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();
 
;