读取字符文件时,如果中途我们想输出字符串要特别小心。
下面的代码在输出文本内容时,很可能产生中文读取再输出的乱码:
private static void byteBuffer() throws IOException {
RandomAccessFile randomAccessFile = new RandomAccessFile("/Users/zhengwei/lanqiao/ConditionOperator.java", "rw");
FileChannel channel = randomAccessFile.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(10);
int bytesRead = channel.read(buffer);
while (bytesRead != -1) {
// System.out.println("读取字节数:"+bytesRead);