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?