1

我正在尝试使用 NIO 使用 transferFrom 从几个较小的文件中组装一个文件。

对 transferFrom 的调用返回 0。没有例外。没有做任何事情来打开同步行为。

    FileOutputStream fos = new FileOutputStream(path);
    FileChannel fileBeingAssembled = fos.channel();
    int progressiveOffset = 4096;
    FileInputStream fis = new FileInputStream(tmpT5);
    FileChannel channel = fis.getChannel();
    channel.position(0);
    int thisItemLength = (int)channel.size();
    LOG.info("Writing " + tag + " at " + progressiveOffset + " length " + thisItemLength);
    fileBeingAssembled.position(progressiveOffset);
    long x = fileBeingAssembled.transferFrom(channel, progressiveOffset, thisItemLength);
    LOG.info("transferred " + x);
    progressiveOffset += thisItemLength;

示例日志:

4409 [main] INFO  com.basistech.seg.writing.ModelOutputTask  - available 1856216
4409 [main] INFO  com.basistech.seg.writing.ModelOutputTask  - Writing word at 15024620 length 1856216
4419 [main] INFO  com.basistech.seg.writing.ModelOutputTask  - transferred 0
4

1 回答 1

2

最明显的两个答案是:

  1. tmpT5 指向一个零字节文件,或者
  2. path 指向的文件长度小于 4096 字节。

来自transferFrom文档:

如果给定位置大于文件的当前大小,则不传输任何字节。

于 2010-01-23T20:20:52.413 回答