Java: possible to line break in a properties file?

JavaConfigurationProperties

Java Problem Overview


Is it possible to continue a long string on the next line in a Java properties file?

e.g., somehow

myStr=Hello
      World

and when I get getProperty("myStr") it will return with "Hello World"?

Java Solutions


Solution 1 - Java

A backslash at the end of a line lets you break across multiple lines, and whitespace that starts a line is ignored:

myStr = Hello \
        World

Note: the backslash needs to be at the very end of the line; it must be the last character, no spaces after it, etc.

The Java docs put it this way:

> A logical line holds all the data of a key-element pair, which may be spread out across several adjacent natural lines by escaping the line terminator sequence with a backslash character \. > > ... > > If a logical line is spread across several natural lines, the backslash escaping the line terminator sequence, the line terminator sequence, and any white space at the start of the following line have no affect on the key or element values.

Solution 2 - Java

myStr = Hello \
        World

The backslash tells the application to continue reading the value onto the next line. ^^

Solution 3 - Java

You need to use \n\ as a solution.

First two symbols \n - new line for string, third \ - multi-line in properties file.

For example (in application.properties):

mail.bodyText=Hello.\n\
This is notification.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeteView Question on Stackoverflow
Solution 1 - JavaJohn FlatnessView Answer on Stackoverflow
Solution 2 - JavaKentView Answer on Stackoverflow
Solution 3 - JavaOleg PoltoratskiiView Answer on Stackoverflow