Search code examples
javafilewriterjco

Unexpected line break using PrintWriter


I don't know why PrintWriter break the line when it see this symbol '##'

I want to write these line to .txt file

Data from system

But got this, you can see it got some unexpected rows:

Result after using Printwriter

Part of my code:

try(OutputStreamWriter fw = new OutputStreamWriter(
                                            new FileOutputStream(filePath + "\\" +file_number + "_"+ info.getFileName(), true),
                                            StandardCharsets.UTF_8);
                                            BufferedWriter bw = new BufferedWriter(fw);
                                            PrintWriter out = new PrintWriter(bw, false)) {
       JCoTable rows = function6.getTableParameterList().getTable("DATA");
       for (int i = 0; i < rows.getNumRows(); i++) {
          rows.setRow(i);
          out.write(Integer.toString(CURRENT_ROW) + ((char)Integer.parseInt(info.getFileDelimited()))+ rows.getString("WA") + "\n");}

Solution

  • The ABAP table contains 6 rows and your output contains 6 rows. So I guess you mean with "additional rows" the 2 additional line breaks in rows no. 2 and 6.

    I assume this is because these line breaks are part of the text in these rows. The SAP GUI doesn't write these control characters and outputs them as '##', but the Java code prints these characters, of course. I guess these 2 substituted chars '##' actually are the control characters END OF LINE (U+000A) and CARRIAGE RETURN (U+000D). You can check their real character codes with the ABAP debugger's hexadecimal view or in your Java development environment debugger.