Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continuous File reading [duplicate]

Tags:

java

file

loops

eof

I have very different scenario in my application. I need to read some text file and do some some work on that.

Actually what I want is, when I reach to the end of line, my file reading cursor in Java will stop there and wait until the new line append in the text file. Actually the file which I am reading is a real time logs which are generated by the server, the log will generate after every one second.

So I want that my file reading process will never end, it keep on reading file as new data comes in the file.

I have written this code,

 try {
        Scanner sc = new Scanner(file);

        while (sc.hasNextLine()) 
        {
            String i = sc.nextLine();

            processLine(i);    //a method which do some stuff on the line which i read
            if(thread==1)
            {
             System.out.print("After loop in the file"); //just for printing
            }
            while(!(sc.hasNextLine()))     //in the loop until new line comes 
                {
                    System.out.println("End of file");
                    Thread.sleep(100000);
                    System.out.println("Thread is waiting");
                    thread++;
                }
            if(thread==1)
                {   
                    System.out.println("OUt of the thread");
                }
   //System.out.println(i);
        }
        sc.close();
        System.out.println("Last time stemp is "+TimeStem);
        System.out.println("Previous Time Stemp is "+Previous_time);
    } 
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }

This is my code, I think my program will remain in the second while loop when end of file reach and when file append it start reading file again. But it's not happening, when end of file is reached, my program wait for some second but never read the next line which is appended in the file.

like image 809
Malik Ehtasham Avatar asked May 31 '26 09:05

Malik Ehtasham


1 Answers

public class LogReader {
    private static boolean canBreak = false;

    public static void startReading(String filename) throws InterruptedException, IOException {
        canBreak = false;
        String line;
        try {
            LineNumberReader lnr = new LineNumberReader(new FileReader(filename));
            while (!canBreak)
            {
                line = lnr.readLine();
                if (line == null) {
                    System.out.println("waiting 4 more");
                    Thread.sleep(3000);
                    continue;
                }
                processLine(line);
            }
            lnr.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void stopReading() {
        canBreak = true;
    }

    private static void processLine(String s) {
        //processing line
    }
}
like image 160
Autocrab Avatar answered Jun 02 '26 22:06

Autocrab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!