简体   繁体   中英

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.

For now I use com.fasterxml.jackson.core.JsonGenerator to write objects to json file.

For reading from file I'm using 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?

My business scenario:

I read records from one big file ( more then couple of GB) and I need to create relations between records. 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.

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:

{
"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. 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).

For multiple files, and possibly multiple accesses to the same file one needs a managing class.

The class Files is usefull for loading/saving.

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. For instance if the json files were really huge. However then the json format is inadequate itself.

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
  • for reading and writing best approach will be use of FileChannel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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