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.");
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With