0

For my application, I need to continuously read from a file and the application will proceed on reading 100 from that file. I'm writing to the same line of the file i.e I'm overwriting the file contents. But my program reads the next line in after each iteration of the loop. My code:

public class h{
  private int flag=0;
  public void scan() 
  {
    String filename="file1.txt";
    try{
      int i,j;
      int nooflines=1;
      String textData = new String();
      try{          
        FileReader fr = new FileReader(filename);
        BufferedReader textReader = new BufferedReader(fr);
        while(flag==0){
          textData=textReader.readLine();
          if(textData==null){
            Thread.sleep(3000);
            continue;
          }
          process(textData);
        }
        textReader.close();
      }catch(InterruptedException e){
        System.out.println(e.getMessage());
      }
    }catch (IOException e){
      System.out.println(e.getMessage());
    }        
  }
  public void process(String data){
    if(data.equals("100")){
      System.out.println(data);
      flag=1;
    }
  }

}

So after one iteration my code will be scanning the second line, but the file1.txt is opened using write mode(-w) which erase its contents and writes at the beginning of the file. So how can I edit my program to keep scanning the first line of the file only?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
sgu
  • 37
  • 7
  • Duplicate of http://stackoverflow.com/questions/262618/java-bufferedreader-back-to-the-top-of-a-text-file I think. It looks like you can either create a new reader or use a RandomAccess reader. – Jeff Watkins Mar 20 '15 at 15:11
  • 2
    I am not sure I fully understand what you are trying to achieve. Can you provide a clear example? – Jean Logeart Mar 20 '15 at 15:11
  • @JeanLogeart I just want to read the file from the beginning in each iterations of while() loop. – sgu Mar 20 '15 at 15:14
  • @sgu Can you explain *why* you want to do this? What is the purpose of your program? – Duncan Jones Mar 20 '15 at 15:15
  • @Duncan I'm calling this class by creating an object in a swing program. When I press a button it(this program) should scan the file untill it reads 100 from it. So after reading 100 I'll extend the program to start a new virtual machine using Ubuntu virtual machine manager. – sgu Mar 20 '15 at 15:20
  • @sgu I still don't really understand what your program needs to do. Can you [edit] your question and try and improve your description? Maybe give us some sample data, e.g. what the file might look like. – Duncan Jones Mar 20 '15 at 15:31

2 Answers2

0

To read your file from the beginning every 3s:

ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
ses.scheduleAtFixedRate(new Runnable() {
    @Override
    public void run() {
        try(BufferedReader br = new BufferedReader(new FileReader(filename))) {
            while(keepReading) {
               keepReading = process(br.readLine());
            }
        }
    }
}, 0, 3, TimeUnit.SECONDS); // every 3s

public boolean process(String data) {
    // do something
    return data != null && !data.equals("100");
}
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
  • Which package should I import to use `ScheduledExecutorService`? I have imported `import java.util.*;` still I'm getting this error `cannot find symbol symbol : class ScheduledExecutorService`. I'm getting same error for `Executors` and `TimeUnit` . – sgu Mar 20 '15 at 17:48
  • They are in ``java.util.concurrent`` – Jean Logeart Mar 20 '15 at 18:01
0

I think this'll do it.

    BufferedReader textReader;
    while(flag==0){
      textReader = new BufferedReader(new FileReader(filename));
      textData=textReader.readLine();
      if(textData==null){
        Thread.sleep(3000);
        continue;
      }
      process(textData);
    }
Sanira
  • 334
  • 3
  • 7