1 回答

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超4個(gè)贊
試試這個(gè):
public static String readFile(File f, int bufSize) {
ReadableByteChannel rbc = FileChannel.open(Paths.get(f),StandardOpenOption.READ);
char[] ca = new char[bufSize];
ByteBuffer bb = ByteBuffer.allocate(bufSize);
StringBuilder sb = new StringBuilder();
while(rbc.read(bb) > -1) {
CharBuffer cb = bb.asCharBuffer();
cb.flip();
cb.get(ca);
sb.append(ca);
cb.clear();
}
return sb.toString();
}
ca如果按 char 寫(xiě)入 char 在性能方面是可以接受的,那么您可以不使用中間人緩沖區(qū)。在這種情況下,您可以簡(jiǎn)單地sb.append(cb.get()).
添加回答
舉報(bào)