Copy a file with InputStream and OutputStream
The following code uses the InputStream and OutputStream to do the byte level file copy.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class Main {
final static int BUFF_SIZE = 5 * 1024 * 1024; // 5MB
final static byte[] buffer = new byte[BUFF_SIZE];
public static void main(String[] arg) throws IOException {
copy(new File("s"), new File("t"));
}
public static void copy(File s, File t) throws IOException {
InputStream in = new FileInputStream(s);
FileOutputStream out = new FileOutputStream(t);
while (true) {
int count = in.read(buffer);
if (count == -1)
break;
out.write(buffer, 0, count);
}
out.close();
in.close();
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
IO File:
- Compare File Dates
- Compress files using with ZIP
- Concatenate files
- Copy a File with NIO FileChannel and ByteBuffer
- Copy a file with FileReader and FileWriter
- Copy a file with InputStream and OutputStream
- Copy a file and overwrite
- Delete a file
- Delete File Recursively
- Get readable file size
- Move a file
- Rename a file
- Report a file's status
- Search a file by regular expressions
- Touch a file: set File Last Modified Time