简体   繁体   English

使用 Java NIO 读写大文件(>1Gb)合适吗?

[英]Reading and Writing large files (>1Gb) using Java NIO suitable?

i am trying to read and write a large file typically more than 1 Gb files in each transaction to another storage location.我正在尝试将每个事务中通常超过 1 Gb 文件的大文件读写到另一个存储位置。 what is the most efficient in terms of performance and low memory (preventing any OOM situation) to transfer the file by writing it to another storage location?就性能和低内存(防止任何 OOM 情况)而言,通过将文件写入另一个存储位置来传输文件的最有效方法是什么?

i saw Java NIO is a more efficient way as compared to traditional Java IO but is it really suitable in my use case and will it be an overkill?我看到与传统 Java IO 相比,Java NIO 是一种更有效的方式,但它真的适合我的用例吗?它会不会有点矫枉过正?

I have tried copying large files of this size (1GB).我曾尝试复制这种大小 (1GB) 的大文件。 The time taken depends more on the underlying hardware and network status than Java NIO itself.花费的时间更多地取决于底层硬件和网络状态,而不是 Java NIO 本身。 As a simple example that I tried just now, copying a file of 989M took the following average timings.作为我刚刚尝试的一个简单示例,复制一个 989M 的文件需要以下平均时间。

Note: There was not much difference when the file already existed and needed to be replaced.注意:文件已经存在需要替换时没有太大区别。

  1. From an SSD folder to an SSD folder: 4ms从 SSD 文件夹到 SSD 文件夹:4ms
  2. From an SSD folder to an HDD (5400rpm) folder: ~7ms从 SSD 文件夹到 HDD (5400rpm) 文件夹:~7ms
  3. From an HDD(5400rpm) folder to an SSD folder: 4ms从 HDD(5400rpm) 文件夹到 SSD 文件夹:4ms
  4. From an HDD(5400rpm)folder to an HDD (5400rpm) folder: ~7ms从 HDD(5400rpm) 文件夹到 HDD (5400rpm) 文件夹:~7ms

I observed the following:我观察到以下情况:

  1. That reading is not causing much of a difference.该读数不会造成太大差异。
  2. That replacing an existing file with new contents is not causing much difference.用新内容替换现有文件不会造成太大差异。
  3. That there is a difference in writing based on the hard disk.基于硬盘的写入存在差异。

When we run tests on network drives, I am sure there will be great variation due to network variables.当我们在网络驱动器上运行测试时,我相信由于网络变量,会有很大的变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM