Copy a File with NIO FileChannel and ByteBuffer
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Main {
private static final int BSIZE = 1024;
public static void main(String[] args) throws Exception {
FileChannel in = new FileInputStream("source").getChannel();
FileChannel out = new FileOutputStream("target").getChannel();
ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
while (in.read(buffer) != -1) {
buffer.flip(); // Prepare for writing
out.write(buffer);
buffer.clear(); // Prepare for reading
}
}
}
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