mmap
public static void main(String[] args) throws IOException {
File file = new File("data.txt");
RandomAccessFile rw = new RandomAccessFile(file, "rw");
MappedByteBuffer mappedByteBuffer = rw.getChannel()
.map(FileChannel.MapMode.READ_WRITE, 0, 2048);
mappedByteBuffer.put("test".getBytes());
rw.close();
}
sendFile
public static void main(String[] args) {
File file = new File("data.txt");
try (FileInputStream fileInputStream = new FileInputStream(file)){
FileChannel inputStreamChannel = fileInputStream.getChannel();
FileOutputStream fileOutputStream = new FileOutputStream("data_copy.txt");
FileChannel outputStreamChannel = fileOutputStream.getChannel();
inputStreamChannel.transferTo(0, file.length(), outputStreamChannel);
} catch (IOException e) {
throw new RuntimeException(e);
}
}