Moon
Moon

Reputation: 23

Java, Read empty line

How can I read the empty line This code doesn't add the empty line, I tried this code

BufferedReader r2 = new BufferedReader(new FileReader("Flags.txt"));
  StringBuffer sb = new StringBuffer();
  while ((r2.readLine()) != null) {
         sb.append(r2.readLine());
         sb.append("\r\n");
  }

      String str = sb.toString();
      System.out.println(str);

outputs

    Dubai
    Qatar
    UAE
    United States

It should be like this

    Dubai
    Qatar
    UAE

    United States

Upvotes: 1

Views: 55

Answers (1)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79620

You are calling readLine() twice. Do it as follows:

String line = "";
while ((line = r2.readLine()) != null) {
     sb.append(line);
     //...
}

Upvotes: 3

Related Questions