Java ResourceBundle leading whitespace in values
Asked Answered
K

2

32

I'm trying out the example given at Oracle's documentations. The java file is here as shown in that page. Now with this java file, I keep a LabelsBundle.properties file in the same directory.

# This is the default LabelsBundle.properties file
s1 = computer
s2 = disk
s3 = monitor
s4 = keyboard

it seems that the trailing whitespace is not ignored while reading the values and the leading whitespaces are ignored.

So if I want leading spaces to be there, what do I do?

In other words, what do I need to do to read the value of s1 as

 computer

instead of

computer

?

Kerseymere answered 28/5, 2012 at 10:59 Comment(0)
S
43

A Unicode escape sequence \u0020 should do it, because the properties parser should recognise it as an actual value and the Java String should interpret \u0020 as the Unicode code point for space.

Alternatively, IIRC, you can escape the space with a backslash like this \ my value. Works for keys, too.

Soissons answered 28/5, 2012 at 11:2 Comment(4)
\u0020 would work without any encoding change? and @Zaki: I tried and without changing any encoding, `` worked !?Kerseymere
Yes, because \u0020 is code. In fact, this is what the native2ascii tool does: convert all characters outside the ASCII range to such Unicode escape sequences to ensure the properties file loads properly without breaking characters when the properties code itself is treated as ASCII.Soissons
also, can I do it without changing the properties file? In java code I mean?Kerseymere
If you mean, configuring the Properties parser to not strip leading whitespace then no. Unless you roll your own and use that, but to me that seems way more effort than editing the properties file and rather pointless.Soissons
A
20

You can escape space with backlash:

s1 =\ computer
Anonymous answered 28/5, 2012 at 11:3 Comment(1)
also, can I do it without changing the properties file? In java code I mean?Kerseymere

© 2022 - 2024 — McMap. All rights reserved.