本文演示了普通的IO操作和NIO操作读取文件,NIO需要注意一下三点:
- NIO是使用两个指针来读取文件,一个postion一个limit,read和write都会改变position的位置,每次使用完,需要交换position和limit
- NIO的乱码问题
- NIO配合channel使用,别忘了关闭Channel,clear Bytebuffer
package com.halfrest.nio.file;
import java.io.*;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
public class RandomAccessFileTest {
public void write(String path) throws IOException {
File file = new File(path);
System.out.println(file.exists());
FileOutputStream fileOutputStream = new FileOutputStream(file,false);
fileOutputStream.write("hahaha".getBytes());
fileOutputStream.flush();
fileOutputStream.close();
}
public void writeNIO(String path) throws IOException {
File file = new File(path);
RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
FileChannel channel = randomAccessFile.getChannel();
//重置channel到0,如果是第一次启动,不用重置
channel.position(0);
ByteBuffer buffer = ByteBuffer.allocate("xhh is yyds".getBytes("UTF-8").length);
CharBuffer decode = StandardCharsets.UTF_8.decode(buffer);
//position会到最后
decode.append(new String("xhh is yyds".getBytes("UTF-8")));
//交换 position limit
decode.flip();
ByteBuffer encode = StandardCharsets.UTF_8.encode(decode);
channel.write(encode);
channel.force(true);
channel.close();
buffer.clear();
}
public String read(String path) throws IOException {
File file = new File(path);
RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
byte[] bytes = new byte[(int) file.length()];
randomAccessFile.readFully(bytes);
return new String(bytes);
}
public String readNIO(String path) throws IOException {
File file = new File(path);
RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
FileChannel channel = randomAccessFile.getChannel();
ByteBuffer buffer = ByteBuffer.allocate((int) file.length());
channel.read(buffer);
//position走到最后面,需要swap position limit
buffer.flip();
//使用UTF-8解码,
CharBuffer decode = StandardCharsets.UTF_8.decode(buffer);
channel.close();
buffer.clear();
return decode.toString();
}
public String readNIODecodeMistake(String path) throws IOException {
File file = new File(path);
RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
FileChannel channel = randomAccessFile.getChannel();
ByteBuffer buffer = ByteBuffer.allocate((int) file.length());
channel.read(buffer);
buffer.flip();
//使用UTF-8解码
CharBuffer decode = buffer.asCharBuffer();
channel.close();
buffer.clear();
return decode.toString();
}
public static void main(String args[]) {
RandomAccessFileTest randomAccessFileTest = new RandomAccessFileTest();
try {
randomAccessFileTest.write("haha.txt");
String read = randomAccessFileTest.read("haha.txt");
System.out.println("read =>"+read);
randomAccessFileTest.writeNIO("haha.txt");
System.out.println("readNIO=>"+randomAccessFileTest.readNIO("haha.txt"));
System.out.println("readNIO readNIODecodeMistake=>"+randomAccessFileTest.readNIODecodeMistake("haha.txt"));
} catch (IOException e) {
e.printStackTrace();
}
}
}