用io包读取中文正确,换成NIO的就成乱码了,,publicclassNIO{privatestaticintBSIZE=1024;publicstaticvoidmain(String[]args)throwsIOException{//写文件FileChannelfc=newFileOutp...
用io包读取中文正确,换成NIO的就成乱码了,,
public class NIO {
private static int BSIZE = 1024;
public static void main(String[] args) throws IOException {
//写文件
FileChannel fc = new FileOutputStream("f:\\aaa.txt").getChannel();
fc.write(ByteBuffer.wrap("asdasdxzcxzc放到".getBytes()));
fc.close();
// //添加文件到末尾
// fc = new RandomAccessFile("f:\\aaa.txt","rw").getChannel();
// fc.position(fc.size());
// fc.write(ByteBuffer.wrap("田间".getBytes("utf-8")));
// fc.close();
//读文件
FileChannel fc2 = new FileInputStream("f:\\aaa.txt").getChannel();
//分配缓存
ByteBuffer buff = ByteBuffer.allocate(BSIZE);
//读入缓存区
fc2.read(buff);
buff.flip();
while(buff.hasRemaining()){
System.out.print(buff.get() + " ");
}
fc2.close();
}
}
展开