1.FileChannel
1.读的例子
public static void read(String fileName) throws IOException {
File file = new File(fileName);
FileInputStream inputStream = new FileInputStream(file);
FileChannel fileChannel2=inputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(2);
int read = fileChannel2.read(byteBuffer);
while (read!=-1){
byteBuffer.flip();
System.out.println(Charset.forName("GBK").decode(byteBuffer).toString());
byteBuffer.clear();
read = fileChannel2.read(byteBuffer);
}
DirectBuffer directBuffer=(DirectBuffer)byteBuffer;
directBuffer.cleaner().clean();
fileChannel2.close();
inputStream.close();
}
2.写的例子
public static void write(String fileName) throws IOException {
File file = new File(fileName);
FileOut