import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
public class Main {
public static void main(String[] args) throws IOException{
File file = new File("./data.txt");
file.createNewFile();
FileChannel fc = new FileOutputStream("./data.txt").getChannel();
fc.write(ByteBuffer.wrap("Some text".getBytes("UTF-8")));
fc.close();
fc = new FileInputStream("./data.txt").getChannel();
ByteBuffer buffer = ByteBuffer.allocate(24);
fc.read(buffer);
buffer.flip();
System.out.println(buffer.asCharBuffer());
}
}
当使用UTF-8进行编码时,最终输出的是乱码。如果使用UTF-16BE却可以正常输出。我系统默认的编码格式是UTF-8,请各位路过的大佬可以帮忙解答一下。