用FileChannel读文件到String的时候,第二次调用这个方法的时候,缓冲器错误
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
//System.out.println(filePath);
try {
FileInputStream fis = new FileInputStream(path);
FileChannel fc = fis.getChannel();
int sz = (int) fc.size();
//System.out.println(sz);
char[] chs = new char[sz];
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
CharBuffer cb = decoder.decode(bb); cb.get(chs);
webFileContent = new String(chs);
fc.close();
fis.close();
} catch (Exception e) {
System.out.println(e);
}
debug是在红色地方报错,错误信息是
java.nio.charset.MalformedInputException: Input length = 1
google了也没有找到有用的信息,看了jdk的api使用clear()或者reset()方法也没有效果,不知道这个是怎么使用的呢
另外如果不用这个方法读,用FileInputStream读的或读出来的都是 方块块……
彻底无语了,sun咋就不能提供个直接用的接口呢。。。