打印写入器println:未创建新行 [英] Printwriter println: no new line created

查看:27
本文介绍了打印写入器println:未创建新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ApachePOI类将Outlook.MSG文件解码为文本文件。

除了PrintWriterprintln方法:它不会创建新行之外,其他一切都运行正常。

它只是将每个句子直接连接在一起。下面的代码片段的结果是

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

我在几台机器上测试了该代码:它在我本地的Tomcat安装程序(Windows计算机)上运行,但在Solaris平台上的Tomcat或WebLogic安装程序上失败。我以为和编码算法有关,所以我用PrintStream代替了Printwriter,表示编码是ISO-8859-1,但也不走运。

有什么想法吗?

    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.");
        }

        }

推荐答案

更改以下内容:

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

这与PrintWriter.println()如何根据操作系统生成Line break有关。对于Unix系统,是LF( ),对于Windows是CR+LF( )

请注意我是如何添加" "表示CR+LF,并使用print()而不是println()。这样生成的换行符与平台无关。

您还可以将以下方法添加到类中以避免重复,只需调用此自定义的println(),而不是直接调用txtOut.print()。

private static final String LINE_SEPARATOR = "
";

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

这样您只需调用println()方法。

这篇关于打印写入器println:未创建新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆