Нужно записать строку, для этого использую FileChanel and 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();
}
Сделал немного по иному, но скорость все равно не снизилась.
public void write(String path, String string) throws IOException {
FileChannel ch = setFileChanel(path, MODE_RW);
ch.write(setByteBuffer(string));
ch.close();
}
private FileChannel setFileChanel(String path, String mode) throws FileNotFoundException {
return new RandomAccessFile(new File(path), mode).getChannel();
}
private ByteBuffer setByteBuffer(String string){
return ByteBuffer.wrap(string.getBytes());
}
Сборка персонального компьютера от Artline: умный выбор для современных пользователей