Good day! Tell me, please, how to implement the next project in java. There is a text file, something is written in it. I have to count it and display it. But what's the problem! It is necessary to implement so that, this program was included until it is closed by the user. If something new was added to the text file and saved, it should immediately appear on the program screen. If nothing is added, then the program works on. I do not ask for the implementation of the entire task, tell me how to implement the last 3 sentences.
- 2the most banal - check the date of the last modification with a timer, if it has changed - reload the file. This is if without binding to any operating system. But if, for example, there is a task for Linux, then inotify can be used there - notifications will be sent if the file has changed. - KoVadim
- I would not want to re-upload the file, and after adding and saving, in the window it showed what was added - Sergey4590
- You need to do this diff. But you still have to download the file. Of course, in some axes there are api, which say which part of the file has changed, but there is a limit to everything. - KoVadim
|
1 answer
You can, everything is possible.
There is such a thing java.nio.file.WatchService - which monitors the status of the file. Here is the dock: Watching a Directory for Changes
It turns out the event model
- Please explain the meaning of the event model. - Sergey4590
- uh ... something happens, an event is being formed, you are processing this event, doing some actions. Those. you do not drive a loop in the stream, with a condition check, but subscribe to a certain event. Shl: I have not yet gotten to the source code of this thing, I don’t say how it works inside (maybe the cycle is chasing). But it works quite comfortably - JEcho
- Yes, in Russian, say - event model. An event rises a certain listener / listener, in which something can be done - Barmaley
- Well, tochnyak ... in Russian, not "HiTech", but "VysTeh", not "Listener", but "Slauhch" =) .. jokes .. I myself do not like bird language, but both in everyday life and in words ) - JEcho
|