public static void method1() {
RandomAccessFile aFile = null;
try {
aFile = new RandomAccessFile("D:\\\\var\\\\demo.txt", "rw");
FileChannel fileChannel = aFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024);
int bytesRead = fileChannel.read(buf);
System.out.println(bytesRead);
while (bytesRead != -1) {
buf.flip();
while (buf.hasRemaining()) {
System.out.print((char) buf.get());
}
buf.compact();
bytesRead = fileChannel.read(buf);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (aFile != null) {
aFile.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
RandomAccessFile使用示例
猜你喜欢
转载自blog.csdn.net/qq_37769323/article/details/104194751
今日推荐
周排行