我正在尝试使用文件通道来读取一个大的 xml 文件,这里是我在这里找到的示例代码。当我尝试它时,它打印出无法识别的字符: import java.io.File; 导入java.io.File;导入 java.io.FileInputStream;导入 java.nio.ByteBuffer;导入 java.nio.channels.FileChannel;
public class MainClass {
public static void main(String[] args) throws Exception {
File aFile = new File("charData.xml");
FileInputStream inFile = null;
inFile = new FileInputStream(aFile);
FileChannel inChannel = inFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(48);
while (inChannel.read(buf) != -1) {
System.out.println("String read: " + ((ByteBuffer) (buf.flip())).asCharBuffer().get(0));
buf.clear();
}
inFile.close();
}
}
输出:
String read: ⸮
String read: ⸮
String read: ⸮
String read: ⸮
String read: ⸮
String read: ⸮
String read: ⸮
String read: ⸮
String read: ⸮
你错过了什么吗?谢谢,
大卫