1

Hi guys I am writing a small application in java where it reads a text file and parses the contents. My problem is my program will keep on reading the file because its inside a loop. My solution was to implement similar to what I usually do in linux like (see below)

ls -l -ctime 
or 
ls -l -atime

where it detects when the files was last accessed or written If you solution can be done in java or some windows command can you please let me know. Thanks

Update:

Thanks very much for helping

Westons Solution

public static long checkLastFileModification() throws InterruptedException{
    
    File file = new File(fileName);
    long lastProcessed = -1;
    
    while (true){
        long lastModified = file.lastModified();
        
        if (lastProcessed != lastModified){
            lastProcessed = lastModified;
            System.out.println("Last accessed : " + lastModified);
            //read the file in reverse order
            
        }
        else{
            System.out.println("No file access.... sleeping for 5 secs ");
            Thread.sleep(5000);
        }
    }
    
}
Community
  • 1
  • 1
dimas
  • 2,487
  • 6
  • 40
  • 66
  • you could try taking a hash of the file, to definitively test for changes, but it probably would't be a good idea if you have large files, or a lot of them: http://stackoverflow.com/questions/304268/getting-a-files-md5-checksum-in-java – robert Feb 04 '15 at 12:32

3 Answers3

1

If you want to show it every single loop, then make a batch file named "loop.bat" in the same folder the project/program is. loop.bat must have the following contents:

@echo off
goto loop
:loop
cls
title Seeing file's contents.
type filename.txt
goto loop

And rename "filename.txt" with the text file you want to see. Now make a java code to show text that it has a batch file running seeing the contents of the file. Or if you can, print the text on the batch file to the java.

filename.txt, loop.bat and your java program must be in the same folder.

  • Thanks for answering NunoLava, I already know how to read the file from windows using java. What i need is how to determine if the text file was recently updated. Like detecting the difference in timestamps. – dimas Feb 02 '15 at 11:30
  • Oh well dimas. I think you need the ".LOG" txt file easter egg. but im pretty sure .LOG text file trick will not work. I have no idea now. But a detector (like the one i made) solves the problem. The exception is that it doesn't show last edited. –  Feb 02 '15 at 11:33
1

You can inspect the last modified time of the file:

new File(fileName).lastModified()

In your loop you can see if it is different:

long lastProcessed = -1;
File file = new File(fileName);
while(true) {
   long lastModified = file.lastModified();
   if (lastProcessed != lastModified) {
      lastProcessed = lastModified;
      process(file);
   }
   else {
     //some delay or sleep, else you're thrashing the thread and file
   }
}
weston
  • 54,145
  • 21
  • 145
  • 203
1

I would suggest to use WatchService API.See in below exmple.

http://java.dzone.com/news/how-watch-file-system-changes

See API :- WatchService

dReAmEr
  • 6,986
  • 7
  • 36
  • 63