« ding dong the witch is gone | Main | relax »

Java copy file code

just cause i didnt find code on this fast enough here is code to copy a file using java. It reads the file in while writting it out using buffered readers and writters:

public static void copy(String from, String to) throws IOException{
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(from);
out = new FileOutputStream(to);
int length = 128*10240; // danger!
byte[] bytes = new byte[length];
int read=0;
for(;;){
read=in.read(bytes,0,length);
if(read==-1){
break;
}
out.write(bytes,0,read);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}

Comments (1)

You can use <pre> before and </pre> after your code to make the html display properly, with indention and all.

Post a comment