It's full of feelings now, but it's refreshing. I just want to get rid of the habit of writing directly to Stream.
Channels.java
ReadableByteChannel inChannel = Channels.newChannel(InputStream);
WritableByteChannel outChannel = Channels.newChannel(OutputStream);
ByteBuffer buffer = ByteBuffer.allocate(102400);
for (int readBytes = inChannel.read(buffer); readBytes != -1; readBytes = inChannel.read(buffer)) {
buffer.flip();
outChannel.write(buffer);
buffer.clear();
}
FileChannel1.java
File srcFile = new File(IN);
FileChannel in = new FileInputStream(srcFile).getChannel();
FileChannel out = new FileOutputStream(new File(OUT)).getChannel();
out.write(in.map(FileChannel.MapMode.READ_ONLY, 0, srcFile.length));
FileChannel2.java
FileChannel in = new FileInputStream(new File(IN)).getChannel();
FileChannel out = new FileOutputStream(new File(OUT)).getChannel();
in.transferTo(0, in.size(), out);
Recommended Posts