我需要写一个字符串,为此我使用它FileChanel,IntBuffer以便写入速度很高,但我得到一个错误Exception in thread "main" java.nio.BufferOverflowException- 位置超过限制,但为什么会发生这种情况?和代码:
public void write(String string) throws IOException {
FileChannel fc = new RandomAccessFile(
new File(FILE_NAME), MODE_RW).getChannel();
IntBuffer ib = fc.map(
FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
for (int i = 0; i < string.toCharArray().length; i++) {
ib.put(string.toCharArray()[i]);
}
fc.close();
}
我做了一点不同,但速度仍然没有降低。