开发者

How to read properties file in Greek using Java

I am trying to read from a properties file which have keys in English & valu开发者_StackOverflow中文版es in greek.My code is like this:

public class I18NSample {

   static public void main(String[] args) {

      String language;
      String country;

      if (args.length != 2) {
          language = new String("el");
          country = new String("GR");
      } else {
          language = new String(args[0]);
          country = new String(args[1]);
      }

      Locale currentLocale;
      ResourceBundle messages;

      currentLocale = new Locale(language, country);

      messages =
        ResourceBundle.getBundle("MessagesBundle",currentLocale, new CustomClassLoader("E:\\properties"));

      System.out.println(messages.getString("greetings"));
      System.out.println(messages.getString("inquiry"));
      System.out.println(messages.getString("farewell"));
   }
}

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;


public class CustomClassLoader extends ClassLoader {

    private String path;



    public CustomClassLoader(String path) {
        super();
        this.path = path;
    }



    @Override
    protected URL findResource(String name) {
        File f = new File(path + File.separator + name);
        try {
            return f.toURL();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return super.findResource(name);
    }

}

MessagesBundle_el_GR.properties greetings=ρήμ. χαιρετώ farewell=επιφ. αντίο

inquiry=τι κάνεισ, τι κάνετε

I am compiling like this javac -encoding UTF8 CustomClassLoader.java javac -encoding UTF8 I18Sample.java

When I run this I get garbled output.If the properies file is in English,French or German it works fine. Please help. Regards, Subhendu


As per the documentation, properties files appear to be restricted to ISO 8859-1. They do however support Unicode escapes, and the linked documentation has a tool to produce said escapes automatically.

The XML loader allows for UTF-8 encoded XML.

The javac options only apply to string literals in the .java file, and do nothing for the runtime behavior of the program.


Thanks theatrus.

I tried to directly read from properties file like this :

FileInputStream fis = new FileInputStream("E:\\properties\\MessagesBundle_el_GR.properties");
        DataInputStream dis = new DataInputStream(fis);
        BufferedReader br = new BufferedReader(new InputStreamReader(dis, "ISO-8859-7"));
        String strLine;
        while((strLine = br.readLine()) != null){
            System.out.println(strLine);
        }

        dis.close();

It did not help.

We can put the same information in database & read from there.Though in the terminal of the machine where mysql database is installed we get the message displayed in Greek properly when we try to read the same from java code it becomes garbled.Will the javac -encoding UTF8 be of help for reading the message from database?

The native2ascii tool will not be of much help as user of our system will not agree to take all that pain.The user is ready to use database( as we will be providing a GUI for entry into database) or properties file.

Regards,


Subhendu

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜