1

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");}
Hau Le
  • 25
  • 1
  • 7

1 Answers1

1

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.

Trixx
  • 1,796
  • 1
  • 15
  • 18
  • I think the problem is Java consider '##' as line breaker if i use this code `rows.getString("WA").replace(System.getProperty("line.separator"), "").toString()` it work but how can i keep '##' without system detect it as line breaker ? – Hau Le Jan 06 '20 at 03:48
  • Java doesn't interpret the '#' characters. It is the SAP GUI screen which doesn't show what is actually there. There are no '#' characters in your data. These are replacement/substitute characters displayed only by the SAP GUI for non-printable characters. Either correct your source data in the ABAP system with removing the control characters (character codepoints < 0x20) or filter them like you did at Java side. But keep in mind that there might also be other control characters in the data. – Trixx Jan 06 '20 at 09:42
  • Furthermore a line break can look different on various OSes. The "line.separator" string from Java side might not always work. See also https://en.wikipedia.org/wiki/Newline . If you would like to be on the safe side you need at least to remove all ASCII characters with codepoint 0x0D and 0x0A separately. – Trixx Jan 06 '20 at 15:40