Hello!
There is such a simple little program. Its purpose: to read the file (the file contains numbers separated by a space), add the numbers to ArrayList, and then output them from the ArrayList to the console. The program constantly checks whether there was a file modification? If the file has been changed, the contents of the ArrayList-a is reset to zero, and then everything is re-read, entered into the ArrayList and output to the console.
class Main { public static void main(String[] args) throws IOException, InterruptedException { File file = new File("res/numbers.txt"); long lastMod = file.lastModified(); // BufferedReader br = new BufferedReader(new FileReader(file)); ArrayList<Integer> numbers = new ArrayList<Integer>(); read(br,numbers); //Был ли изменен файл? while (true){ if(lastMod!=file.lastModified()){ numbers.clear(); //Чистим ArrayList read(br, numbers); //Заново считываем lastMod = file.lastModified(); //Обновляем дату модификации } } } public static void read(BufferedReader br, ArrayList<Integer> numb) throws IOException { String line; while ((line = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(line," \"'!&?,.~"); while (st.hasMoreTokens()){ numb.add(Integer.parseInt(st.nextToken())); } } for(Integer i: numb){ System.out.print(i + " "); } }} The question is:
Let's say there were such numbers in the file: 1 56 88 39 100, the program counted them and output them to the console, it continues to work and is waiting for file changes
I manually open the file, append a space and a new number 1000 to the end of the row, save the file
the console shows us: 1 56 88 39 100 1000, i.e. all OK
BUT , when I go into the file and delete a number (close-save), nothing happens on the console, a number of numbers remain unchanged
Why it happens? How to make it work normally?