Question

I have this code:

String sampleString = "1223,4455,6667" + "\n" + "1223,4455,6667" + "\n";

BufferedReader reader = new BufferedReader(new StringReader(sampleString));
String line;
while ((line = reader.readLine()) != null) {
   if (line.trim().length() == 0) {
        continue;
   }
   logger.debug("CSVLIne:" + line);
}

I was expecting it to print two lines. But it shows all in one line. Anything wrong in this code?

Was it helpful?

Solution 3

You need to escape those newlines

    String sampleString = String.format("1223,4455,6667%n1223,4455,6667%n")

The %n format specifier introduces platform dependent newline.

OTHER TIPS

Its because logger.debug outputs on one line without newlines. If you want to print on two lines you need to use logger.debug twice.

Just tried it myself, and above code prints two lines on my machine when using System.out.println for output.

As others have pointed out, "\n" is not platform independent, but according to the BufferedReader documentation:

A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

it should be ok.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top