Java: opening and reading from a file without locking it.
        Posted  
        
            by rogue780
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by rogue780
        
        
        
        Published on 2010-03-29T10:52:05Z
        Indexed on 
            2010/03/29
            10:53 UTC
        
        
        Read the original article
        Hit count: 357
        
I need to be able to mimic 'tail -f' with Java. I'm trying to read a log file as it's being written by another process, but when I open the file to read it, it locks the file and the other process can't write to it anymore. Any help would be greatly appreciated!
Here is the code that I'm using currently:
public void read(){
    Scanner fp = null;
    try{
        fp = new Scanner(new FileReader(this.filename));
        fp.useDelimiter("\n");
    }catch(java.io.FileNotFoundException e){
        System.out.println("java.io.FileNotFoundException e");
    }
    while(true){
        if(fp.hasNext()){
            this.parse(fp.next());
        }           
    }       
}
© Stack Overflow or respective owner