我有以下代码:
我有以下代码:
String inputFile = "somefile.txt";
FileInputStream in = new FileInputStream(inputFile);
FileChannel ch = in.getChannel();
ByteBuffer buf = ByteBuffer.allocateDirect(BUFSIZE); // BUFSIZE = 256
/* read the file into a buffer, 256 bytes at a time */
int rd;
while ( (rd = ch.read( buf )) != -1 ) {
buf.rewind();
for ( int i = 0; i < rd/2; i++ ) {
/* print each character */
System.out.print(buf.getChar());
}
buf.clear();
}
但是字符显示为问号。这与Java使用Unicode字符有关吗?我该如何纠正这个问题?
buf.flip()
代替buf.rewind()
,并将整个chbuf传递给System.out.print()
。 - hertzsprung