12

I am trying to decode an outlook .MSG file to a text file, using Apache POI classes.

Everything works fine, except for the println method of PrintWriter: it doesn´t create a new line.

It just concatenates every sentence directly one after another. The result of the code snippet below is

"De: textPara: " iso 
"De: "
"Para: "

I tried the code on several machines: it works on my local tomcat instalation (Windows machine), but fails on a tomcat or Weblogic instalation on a Solaris platform. I thought it had something to do with the encoding algorithm, so I used PrintStream in stead of Printwriter, indicating the encoding ISO-8859-1, but no luck neither.

Any idea?

    try {
        byte [] msgByte = Base64.decodeBase64(msgBase64);

        InputStream inputMsg = new ByteArrayInputStream(msgByte);
        msg = new MAPIMessage(inputMsg);

        /* 1. Transform MSG to TXT. */
        try {
            txtOut = new PrintWriter(outputMsg);
            try {
                String displayFrom = msg.getDisplayFrom();
                txtOut.println("De: "+displayFrom);
            } catch (ChunkNotFoundException e) {
                _logger.info("Error extrayendo displayFrom: "+e);
            }
            try {
                String displayTo = msg.getDisplayTo();
                txtOut.println("Para: "+displayTo);
            } catch (ChunkNotFoundException e) {
                _logger.info("Error extrayendo displayTo: "+e);
            }

        } finally {
        if(txtOut != null) {
            txtOut.close();}
        else {
            _logger.error("No se ha podido parsear el mensaje.");
        }

        }
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
nicBBB
  • 267
  • 1
  • 4
  • 18
  • 2
    what are you printing to ? A console, a log file, a text box in a GUI app, html ? – nos Dec 12 '11 at 15:44
  • 1
    Are you trying to add one extra line between each line. Try `txtOut.println()` right after each time you write a line. – Bhesh Gurung Dec 12 '11 at 15:58
  • What is the exact output you are expecting, besides just what is actually output? – Peter Dec 12 '11 at 16:00
  • @gurung: I will try this out. The weird thing is that it seems to work on a tomcat running on Windows. – nicBBB Dec 12 '11 at 16:11
  • After base64, there'll be no newlines. Where/how are you decoding the base64 data so you can view the new lines ? (This sounds like you're encoding on a platform that uses \n as a newline , and decoding/displaying it on a platform that uses \r\n or vice versa) – nos Dec 12 '11 at 16:17
  • 1
    @nos: The end result should be a text file encoded to Base64. outputMsg is of the type ByteArrayOutputStream. I convert the ByteArrayOutputStream in a byte array which I encode to Base64. – nicBBB Dec 12 '11 at 16:21
  • @nos: Encoding happens on a unix platform (tomcat running on Solaris) and decoding happens on another unix platform (SAP ABAP classes also running on Solaris). Could it be a problem of character encoding? In PrintWriter I don´t specify the encoding so I guess it takes the default encoding of the platform. The target platform should have the same character encoding so that during decoding there is no loss o the \n? – nicBBB Dec 12 '11 at 17:01
  • Problem solved change println to print + \r\n. Thanks a lot. – nicBBB Dec 13 '11 at 07:27

1 Answers1

22

Change the following:

txtOut.print("De: "+displayFrom + "\r\n");
txtOut.print("Para: "+displayTo + "\r\n");

This is related to how PrintWriter.println() generates the Line break depending of the Operating System. For unix systems is LF (\n), for Windows is CR+LF (\r\n).

Notice how I added the "\r\n" which means CR+LF and used print() instead of println(). This way the line break generated is not platform dependent.

You can also add the following method to your class to avoid duplicity and just call this custom println() instead of directly calling txtOut.print().

private static final String LINE_SEPARATOR = "\r\n";

public void println(String str) {
    txtOut.print(str + LINE_SEPARATOR);
}

This way you just call println() method.

Alfredo Osorio
  • 11,297
  • 12
  • 56
  • 84
  • 7
    Instead of tagging on "\r\n" a safer method might be to define and use: public static String newline = System.getProperty("line.separator"); – BlueVoid Dec 12 '11 at 16:16
  • 1
    Problem solved changing println to print + \r\n. Thanks a lot. – nicBBB Dec 13 '11 at 07:28
  • actually when is \r\n is platform dependant: it is strictly for Windows OS, in linux that extra char produce noise ^M char. – Raffaello Jul 14 '18 at 12:56