Why am I losing a new line character when loading text from a Java servlet in JTextPane?

I am trying to load the contents of a text file containing some text in several lines using a Java servlet.
When I check the servlet in the browser, it works fine. Text is loaded with new string characters.
But when I load it into a string in my swing application and then use textpane.setText(text);, the new lines will disappear. I tried many solutions that I found on the net, but still can not understand.

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 this some kind of platform issue? The servlet was written and compiled on Linux, but I run it on Windows (on JBoss). Text files are also stored on my machine.

+3
1

data.append("\n")

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

Source: https://habr.com/ru/post/1777425/


All Articles