【发布时间】:2013-05-09 16:08:38
【问题描述】:
这里我有以下来自this oracle java 教程的代码:
// Defaults to READ
try (SeekableByteChannel sbc = Files.newByteChannel(file)) {
ByteBuffer buf = ByteBuffer.allocate(10);
// Read the bytes with the proper encoding for this platform. If
// you skip this step, you might see something that looks like
// Chinese characters when you expect Latin-style characters.
String encoding = System.getProperty("file.encoding");
while (sbc.read(buf) > 0) {
buf.rewind();
System.out.print(Charset.forName(encoding).decode(buf));
buf.flip();//LINE X
}
} catch (IOException x) {
System.out.println("caught exception: " + x);
所以基本上我没有得到任何输出。
我试图在while循环中放置一些标志来检查它是否进入,它是否进入。我还更改了Charset.defaultCharset().decode(buf) 中的编码,结果:没有输出。
当然文件中有文本传递给newByteChannel(file);
有什么想法吗? 提前非常感谢。
**
编辑:
** 已解决,只是我尝试访问的文件之前意外损坏了。更改文件后,一切正常。
【问题讨论】: