1

My program is reading all lines in the file but i just need the second one.

String line;
try (
    InputStream fis = new FileInputStream(source);
    InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
    BufferedReader br = new BufferedReader(isr)) {  
    while ((line = br.readLine()) != null) {
       System.out.println(line);
    }
}
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Kozek
  • 15
  • 1
  • 7

1 Answers1

2

If you only need the second line and you are sure the file always has at least two lines, you can just read twice and ignore the first time.

br.readLine(); //read, but ignore
System.out.println(br.readLine()); // read and output
SilverNak
  • 3,283
  • 4
  • 28
  • 44