English 中文(简体)
Java-通过渠道移交大宗档案
原标题:Java - Transferring big files over channels - NIO
  • 时间:2012-05-20 20:53:46
  •  标签:
  • java
  • nio

我必须利用国家信息管理系统将数据转换到服务器上,但我可以说明如何这样做,而不在任何地方进行转让,以保持转让状态。

我的第一个想法是分配档案的规模,显然我可以把这个大档案的大小寄给,因为它一劳永逸地掌握了援助团。 然后,我想,为什么不仅没有收到任何转让,而且在问题出现时,情况就是如此。

即便是我撰写服务器边的数据,也全部时间。

        FileChannel fc = new FileInputStream(f).getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while(fc.read(buffer) > 0) {
            buffer.flip();
            while(channel.write(buffer) > 0);
            buffer.clear();
        }

但是,由于在档案转让中必须中断时间,有些时间对数据进行不断读,在没有数据的情况下,数据就会中断。

我可以指出,如果仍然有数据可查,而不必将每一数据卷作为带有密码的新包装,我怎么能告诉客户?

我也想知道,是否有比以下更好的办法发出整个缓冲。

while(channel.write(buffer) > 0);
最佳回答

通过缓冲渠道复制的正确方式如下:

while (in.read(buffer) >= 0 || buffer.position() > 0)
{
  buffer.flip();
  out.write(buffer);
  buffer.compact();
}

这涉及所有玉米病例,包括阅读长度!

NB是阻挡模式的。 如果你不设锁,请回到<条码>选择(<>/条码>,如读()或写()零份。

问题回答

或许你们正在寻找:

channel.transferFrom(0, fc.size(), fc);

如果你不能依靠袖珍材料继续开放,那么,在你的议定书中,就没有办法建立恢复和继续机制。 关于向客户说明有多少批量,你可以找到一份档案的大小,而不用读到File.length





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签