import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class ChannelExample { public static void main(String[] args) { try { RandomAccessFile file = new RandomAccessFile("data.txt", "rw"); FileChannel channel = file.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(48); int bytesRead = channel.read(buffer); while (bytesRead != -1) { System.out.println("Read " + bytesRead + " bytes"); buffer.flip(); while (buffer.hasRemaining()) { System.out.print((char) buffer.get()); } buffer.clear(); bytesRead = channel.read(buffer); } channel.close(); file.close(); } catch (Exception e) { e.printStackTrace(); } } }


上一篇:
下一篇:
切换中文