I want to print from a file a specific line, for example the fourth line or the second line. This is my code and it only displays all lines and each lines number. I'm sorry if this is a simple and stupid question, but thank you in advance :D.
FileReader fr = null;
LineNumberReader lnr = null;
String str;
int i;
try{
// create new reader
fr = new FileReader("test.txt");
lnr = new LineNumberReader(fr);
// read lines till the end of the stream
while((str=lnr.readLine())!=null)
{
i=lnr.getLineNumber();
System.out.print("("+i+")");
// prints string
System.out.println(str);
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}finally{
// closes the stream and releases system resources
if(fr!=null)
fr.close();
if(lnr!=null)
lnr.close();
}
}
}