java - 非常快地写入文件
我获得快速数据流(对象),我想将其写入文件。
这是一个独立的进程,因此它不执行任何操作,只是从套接字读取数据,将其解析为 csv 并将所有内容写入文件。
将大量 csv 行写入文件的最佳方法是什么?
缓冲区正在写入我的解决方案吗?
Java 中有缓冲的 File 对象吗?
我应该自己管理并使用 writeLines() 吗?
I get a fast stream of data (objects) and I would like to write it to a file.
This is a stand alone process so it doesn't do anything but read the data from a socket parse it to csv and write all to a file.
What is the best way to write a lot of csv lines to a file?
Is a buffer writing my solution?
Is there a buffered File object in Java ?
Should I manage it myself and use writeLines()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在文本文件 Java 中写入大量数据的最快方法
Fastest way to write huge data in text file Java
如果您正在处理大量数据,那么我建议您使用一组内存缓冲区,在其中存储到达的数据,然后使用一个线程/线程池,它使用 Java NIO“消耗”这些缓冲区并将它们写入磁盘。然而,您将受到磁盘写入速度的限制 - 请记住,网络速度比硬盘速度快的情况并不罕见!因此,您可能需要考虑一个线程池,它在不同的物理位置进行写入,并且仅在接收和写入所有数据后“粘贴”这些文件。
If you're dealing with a huge throughput of data then I suggest you use a set of in-memory buffers where you deposit the data arriving and then have a thread/threadpool which uses Java NIO to "consume" these buffers and write them onto disk. You will however be limited by the disk writing speed -- bear in mind that it's not unusual for the speed of network to be faster than the speed of your hard disk! so you might want to consider a threadpool which writes in different physical locations and only "pastes" these files after all the data has been received and written.
如上所述,很可能是磁盘 I/O 限制了您,而不是 Java 抽象。
但除了使用一个好的库来处理 CSV 之外,您还可以考虑使用其他(甚至更)高效的格式,例如 JSON;以及压缩。 GZIP擅长压缩东西,但速度相对较慢;但也有更快的。例如,LZF(如 这个 Java 实现)足够快,能够以高于典型磁盘 I/ 的速度进行压缩O(并且解压缩速度更快)。因此,压缩输出很可能会增加吞吐量并减少磁盘使用量。
As mentioned above, chances are that its disk I/O that limits you, not Java abstractions.
But beyond using a good lib to deal with CSV, you might consider using other (even more) efficient formats like JSON; as well as compression. GZIP is good at compressing things, but relatively slow; but there are faster ones too. For example, LZF (like this Java implementation) is fast enough to compress at speeds higher than typical disk I/O (and uncompress even faster). So compressing output may well increase throughput as well as reduce disk usage.