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?

    2 answers 2

    You are working incorrectly with I / O, that is, with the BufferedReader class. Here is your code corrected:

     class Main { public static void main(String[] args) throws IOException, InterruptedException { File file = new File("res/numbers.txt"); long lastMod = file.lastModified(); ArrayList<Integer> numbers = new ArrayList<Integer>(); read(file, numbers); // Был ли изменен файл? while (true) { if (lastMod != file.lastModified()) { numbers.clear(); // Чистим ArrayList read(file, numbers); // Заново считываем lastMod = file.lastModified(); // Обновляем дату модификации } } } public static void read(File file, ArrayList<Integer> numb) throws IOException { BufferedReader br = new BufferedReader(new FileReader(file)); //читаем файл заново каждый раз String line; while ((line = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(line, " \"'!&?,.~"); while (st.hasMoreTokens()) { numb.add(Integer.parseInt(st.nextToken())); } } br.close(); //не забываем закрыть поток ввода/вывода for (Integer i : numb) { System.out.print(i + " "); } System.out.println(); //ну и перевод строки для красивости } } 

      BufferedReader does not provide an interface for reading from the beginning (but in this code it is your fault). Therefore, when you add new characters to the file, BefferedReader reads that with the offset of the last character already read.

      Therefore, you have two ways:

      1. Open the file every time (do not forget to close it again later) when you enter the cycle, then with the help of BufferdReader you will read everything from the very beginning (all changes will be included accordingly).
      2. Use RandomAccessFile :

       RandomAccessFile br = new RandomAccessFile(file, "r"); //... while (true){ //.... numbers.clear(); //Чистим ArrayList br.seek(0); //.... }