How do I copy a file?
Date: 2010-09-16. Category: Java I/O examples. Hits: 14K time(s).
This example demonstrate a copy file process using Java IO library, specifically here we use the FileInputStream and its tandem the FileOutputStream class.
package org.kodejava.example.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopyDemo {
public static void main(String[] args) {
//
// Create an instance of source and destination files
//
File source = new File("TS-6623.pdf");
File destination = new File("NEO-TS-6623.pdf");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(source);
fos = new FileOutputStream(destination);
//
// Define the size of our buffer for buffering file data
//
byte[] buffer = new byte[4096];
int read;
while ((read = fis.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//
// Finally close the input and output stream after we've finished with them.
//
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}