Why do I loose new line character when I load text from a java servlet to JTextPane?

[亡魂溺海] 提交于 2019-12-10 10:16:27

问题


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 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();

Sending text:

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

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.


回答1:


Instead of data.append("\n") use

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


来源:https://stackoverflow.com/questions/4302837/why-do-i-loose-new-line-character-when-i-load-text-from-a-java-servlet-to-jtextp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!