P basak
P basak

Reputation: 5004

Constantly monitor dynamically updated files in multi-threaded java program

I want to know what will be the efficient (in terms of time and memory) java code to implement the following:

I need a java program to continuously check two files, the first file let us call it A will have a product name continuously added and another file B will be its corresponding prices from different source. both of the files are being dynamically updated. what the program will do is that, whenever a product is added to A it will add that word to a list and as long as no new product is not added it will consider every new price added to file B from that time to be the prices of the corresponding product. It will then create a mapping of product and prices.

So, here is the file

A

Macbook Pro

Dell XPS

Lenovo

B

(Macbook pro added in file A)

100

200

(Dell XPS is added in file A)

300

400

(Lenovo is added in A)

500

600

700

so it will create a mapping like,

Macbook Pro -> 100,200

Dell XPS -> 300, 400

Lenovo -> 500, 600, 700

The above example is an illustration of what I am trying to achieve. My idea so far is that, keep two thread monitor each file, whenever a product is added to A start counting from B (mark previous position in B), whenever a new product added to A, start from B from the previously marked position. Is this a right approach? And how can I read the dynamic files constantly?

Upvotes: 0

Views: 214

Answers (1)

dngfng
dngfng

Reputation: 1943

I would use Apache Commons-IO FileAlterationObserver, with this you can use Listeners that get notified when the File gets changed.

Upvotes: 2

Related Questions