Search code examples
javaniofilechannel

FileChannel.write incomplete


I'm using FileChannel to wrtie 2MB data to a file.

  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 is called write(2000, 1024)

But only 1048576 bytes are written which should have been 2048000 bytes

# 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

When calling write(1000, 1024), it works fine. 1024000 bytes are written.

What am I doing wrong?


Solution

  • You're assuming that write() is supposed to transfer entire buffer array. It isn't obliged to transfer more than one byte, in actuality. That's why it returns a count; it's also why the Javadoc says it can return zero, and talks about 'the number of bytes actually written'.

    You have to loop.