Annie
Annie

Reputation: 45

Preserve newline while writing data from DB to file

I have a JSP page with a text-area when user clicks save button, it saves the data from the text area to the database. DB field corresponding to data from text-area is Clob . I need to write this data to a file (after converting clob to String), by preserving newline. But when I convert Clob to String, the newlines are missing

Can I preserve newline while converting clob data to String.

Upvotes: 0

Views: 1187

Answers (1)

Moritz Petersen
Moritz Petersen

Reputation: 13057

I assume, that you want to output your text on a JSP page. Therefore, you have to convert it to HTML.

Replace all new line characters with <br>.

String html = text.replace("\n", "<br>\n");

Additionally, you should escape the special characters in your text to HTML entities. Use StringEscapeUtils#escapeHtml4() from the Commons-Lang library.

Upvotes: 1

Related Questions