简体   繁体   English

如何同时写和读文件

[英]How to write and read to file reactively in the same time

Someone knows way to write and read from file (xml, json) in java where java object will be able to store in file in reactive way. 有人知道在Java中读写文件(xml,json)的方法,其中java对象将能够以响应方式存储在文件中。

For now I use com.fasterxml.jackson.core.JsonGenerator to write objects to json file. 现在,我使用com.fasterxml.jackson.core.JsonGenerator将对象写入json文件。

For reading from file I'm using com.fasterxml.jackson.core.JsonParser 为了读取文件,我使用com.fasterxml.jackson.core.JsonParser

but I'm not able to write and read from file in the same time. 但我无法同时读写文件。 I don't want to have many open/close file descriptor operations. 我不想有很多打开/关闭文件描述符操作。

It's way to do it in java? 用Java做到的方式?

My business scenario: 我的业务场景:

I read records from one big file ( more then couple of GB) and I need to create relations between records. 我从一个大文件(多于几个GB)读取记录,并且需要在记录之间创建关系。 Records in input file unfortunetlly are not sorted and it's imposible to write to sort file when all records are in perfect order. 不幸的是,输入文件中的记录不会进行排序,并且当所有记录都处于完美顺序时,就无法写入排序文件。

I iterate over input file and find parent record and want to write the record to second file when I was created realations between records. 我遍历输入文件并找到parent record并希望在创建记录之间的记录时将记录写入第二个文件。

In second step I want to write child record but I need to iterate over record relations file (read file operation with open write descriptor) and I need to find find parent record for my child record and update that parent record when I fill child structure where insert child_1 element example structure used by me right now: 在第二个步骤,我想写child record ,但我需要遍历record relations file (阅读开放写入描述符文件操作),我需要找到找到parent record我的child record和更新父记录时,我填child structure ,其中插入child_1 element示例结构,我现在正在使用:

{
"parent_1": {
...
childs: [{
"child_1": { ... } 
}]
}
}

The description of your problem is not very clear, we don't know why you want to do that and what you want to achieve . 您对问题的描述不是很清楚,我们不知道您为什么要这样做以及您想要实现什么 But i'll have a try: 但我会尝试:

If you try to do something like React.js does in JavaScript, you'd need your program to monitor if the file has changed and react to that by updating the corresponding object. 如果您尝试像JavaScript中的React.js一样执行操作,则需要程序监视文件是否已更改,并通过更新相应的对象来对此做出反应 But this won't be very performant (nor make much sense) - maybe just using static fields in you object's class to maintain state between instances would do the trick? 但这不是很有效(也没有多大意义)-也许仅在对象类中使用静态字段来维护实例之间的状态就可以解决问题? If that applies to your scenario at all... 如果这完全适用于您的情况...

I would load and save a file in its entirety with their file modification times (or a running version number/UUID in the json). 我将使用文件的修改时间(或json中正在运行的版本号/ UUID)完整地加载和保存文件。

For multiple files, and possibly multiple accesses to the same file one needs a managing class. 对于多个文件,可能需要多次访问同一文件,需要一个管理类。

The class Files is usefull for loading/saving. Files类对于加载/保存很有用。

Path path = Paths.get("...");
FileTime t = Files.getLastModified(path);
byte[] data = Files.readAllBytes(path);
FileTime tcheck = Files.getLastModified(path);
ByteArrayInputStream bais = ...

In this way access is as atomic/momentarily as possible. 通过这种方式,访问尽可能地是原子/短暂的。

Ideally one could use FileLock.lock() , but that is more for random-access files, file channels, especially with buffers. 理想情况下,可以使用FileLock.lock() ,但对于随机访问的文件,文件通道(尤其是带有缓冲区的文件FileLock.lock() ,则更多。 For instance if the json files were really huge. 例如,如果json文件确实很大。 However then the json format is inadequate itself. 但是,json格式本身并不足够。

I think the best solution for my case will be: 我认为针对我的情况的最佳解决方案是:

  • read parent record and find for him all child records (store in memory) and after reaching end-of-file flush my parent record + child records to relations_file 读取父记录并为他找到所有子记录(存储在内存中),并在到达文件末尾后将我的父记录+子记录刷新到relations_file
  • for reading and writing best approach will be use of FileChannel 读写最佳方法将使用FileChannel

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

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