0
ファイルチャネルを使用して大きなxmlファイルを読み取ろうとしています。ここではサンプルコードは3210です。私が試してみると、認識できない文字が表示されています: import java.io.File; import java.io.File; import java.io.FileInputStream; import java.nio.ByteBuffer; import 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: ⸮
あなたがここに何を欠場しましたか? おかげで、
デビッド
を試してみてください!別の質問:ここでバイトサイズの問題は起こりますか?私はそれを増やすとパフォーマンスが向上しますか? –
実際には認識できない文字はありませんでしたが、行単位でファイルを読み取っていません –
@DavidZhaoしかし、問題はそれが私が解決した迷惑文字を返す理由です。 –