0

I want to display characters (Chinese or other language) from property file on Windows box.

Let's say I read a property server.location=上海的位置 from System property, which is set when server is started.

I tried to do this

new String(locationStr.getBytes(System.getProperty("file.encoding")), "UTF-8");

This works with Linux, but couldn't get it working on Windows.

Following is summarized snipet, without syntax of how the System Property is set

URL fileURL = new URL("file:filePathAndName");
InputStream iStream = fileURL.openStream () ;
Properties prop = new Properties();
prop.load(iStream);
//Enumerate over prop and set System.setProperty (key, value);

Reading property as System.getProperty("server.location")

This is done centrally for all property files, hence modifying anything while reading or setting specific encoding could affect others, hence not advisable.

Also tried to encode using URLEncoder.encode but didn't help. I do not see any specific encoding set. Java uses UTF-16, on Windows the encoding is 'Cp1252'. What am I missing here?

Any help to make this work or throw some light is appreciated. Also tried to go through existing questions, but the answers didn't apply directly hence creating new question. Thanks

Edit: Couldn't convert the obtained String to UTF-8. Somehow convinced people to read properties in way Joop mentioned and retrieve the String properly

3
  • 1
    A properties file is supposed to be encoded in ISO_8859_1. What does your file actually contain? Commented Apr 27, 2016 at 16:09
  • "UTF-8" in property file sounds confussing ? Commented Apr 27, 2016 at 16:10
  • Hi JB, not sure by what you mean by "file should be encoded in ISO-8859-1" I found in Windows the default is encoding is Cp1252. The property file consists of numerous properties, one per line. eg server.name=someName <new line> server.type=someType <new line> server.location=someLocation etc Commented Apr 27, 2016 at 19:45

2 Answers 2

1

String/char/Reader/Writer in java contain Unicode text. Binary data, byte[], InputStream/OutputStream must be associated with an encoding to be convertable to text, String.

It seems your Properties file is in UTF-8. Then specify a fixed encoding when loading the properties.

InputStream iStream = fileURL.openStream();
Reader reader = new BufferedReader(new InputStreamReader(iStream, StandardCharsets.UTF_8));
Properties prop = new Properties();
prop.load(reader);

Here the InputStreamReader bridges the transition from binary data to (Unicode) text by a conversion specifying the encoding of the InputStream.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Joop for the answer, but want to know what could be the reason for not being able to convert the String to Unicode. As mentioned earlier, modifying the code to read and set System property is little out of reach. Hence want to interpret the String post reading from System Property.
So the problem lies in the passing of the system property. Are you using java -Dserver.location=... ...? Then you are restricted to the operation system encoding of the command line. Java might receive wrong String content, and then it might even be impossible to correct.
Just to add some info the property is mentioned in a file, in which many key value pair, one per line are mentioned. eg. server.name=somename <new line> server.type=stype <new line> server.location=someLocation ... And read by different processes, in one such process, I intend to utilize the String
And that file is in UTF-8, but how? I thought by the code for Properties. On reading one has to specify the encoding of that file, so what is put into the String is text, Unicode,
0
Properties prop = new Properties();
InputStream input = null;
String filename = "config.properties";
input = ClassName.class.getClassLoader().getResourceAsStream(filename);

            //loading properties
            prop.load(input);

            //getting the properties
                System.out.println(prop.getProperty("propertyname1"));
                System.out.println(prop.getProperty("propertyName2"));
                System.out.println(prop.getProperty("propertyName3"));

or you can enumerate over the proerties

 Enumeration e = prop.propertyNames();

    while (e.hasMoreElements()) {
      String key = (String) e.nextElement();
      System.out.println(key + " -- " + prop.getProperty(key));
    }

this is how you should actually get properties from a property file and you dont have to worry about the utf-8 characters.

1 Comment

Thanks Priyamal for the answer. If I could modify the reading code, this would have been useful. But is there a way to convert the String from System Property to proper UTF-8

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.