当我从Java servlet加载文本到JTextPane时,为什么松开换行符? [英] Why do I loose new line character when I load text from a java servlet to JTextPane?

查看:77
本文介绍了当我从Java servlet加载文本到JTextPane时,为什么松开换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用java servlet加载包含多行文本的文本文件的内容.
当我在浏览器中测试servlet时,它工作正常.文本已装入新的换行符.
但是,当我将其加载到我的swing应用程序中的字符串中,然后使用textpane.setText(text);时,新行不见了.我尝试了很多在网上找到的解决方案,但仍然无法解决问题.

I try to load content of a text file that contains some text in multiple lines using java servlet.
When I test servlet in browser it works fine. Text is loaded with new line chars.
But when I load it to a string in my swing application and then use textpane.setText(text); new lines are gone. I tried many solutions I found int the net, but still can't get it right.

Servlet代码:
从文件读取文本(简体):

Servlet Code:
Reading text from file (simplified):

File file = new File(path);
StringBuilder data = new StringBuilder();   
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
while ((line = in.readLine()) != null) {
    data.append(line);
    data.append("\n");
}
in.close();

发送文本:

PrintWriter out = response.getWriter();
out.write(text));

这是平台问题吗? Servlet是在Linux上编写和编译的,但我是在Windows(在JBoss上)上运行的.文本文件也存储在我的机器上.

Is it some platform issue? Servlet was writen and compiled on Linux, but I run it on Windows (on JBoss). Textfiles are also stored on my machine.

推荐答案

代替data.append("\n")使用

data.append(System.getProperty("line.separator"));

这篇关于当我从Java servlet加载文本到JTextPane时,为什么松开换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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