Я использую FileChannel
, чтобы получить данные 2 МБ в файл.FileChannel.write неполное
private void write(int numEntries, int entrySize)
throws Exception
{
File dir = new File(iotestDir);
dir.mkdirs();
File file = new File(dir, "data00001.dat");
file.delete();
file.createNewFile();
try {
FileChannel channel = new FileOutputStream(file).getChannel();
ByteBuffer[] bb = new ByteBuffer[numEntries];
for (int i = 0; i < numEntries; ++i) {
bb[i] = generator.generateRandomSlice(entrySize).toByteBuffer();
}
System.out.println(bb.length);
long position = channel.write(bb);
System.out.println(position);
Thread.sleep(10000);
channel.force(true);
channel.close();
}
catch (Exception e) {
file.delete();
e.printStackTrace();
throw e;
}
}
write
называется write(2000, 1024)
Но только 1048576 байт записываются, которые должны были 2048000 байт
# ls -al
total 1032
drwxrwxr-x 2 lingchuanhu lingchuanhu 4096 Mar 12 13:06 .
drwxrwxr-x 5 lingchuanhu lingchuanhu 4096 Mar 12 11:40 ..
-rw-rw-r-- 1 lingchuanhu lingchuanhu 1048576 Mar 12 13:06 data00001.dat
При вызове write(1000, 1024)
, он отлично работает. Написано 1024 000 байт.
Что я делаю неправильно?