How to copy a file
In Java 5 and 6:
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();
In Java 7:
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);
In Groovy:
new File("/tmp/otherFile") << new File("/tmp/someFile").text
( new AntBuilder ( ) ).copy ( file : 'blah' , tofile : 'fobar' )
No comments:
Post a Comment