开发者

Java Native language Application Doesnt work outside IDE

I have a program developed under java wit开发者_运维百科h netbeans. It has a text pane that takes text written in non English language and do some operation including save open new.....

The program was fine and complete worked flawlessly when i run it from netbeans. But when i go to the dist folder and run the jar (which was supposed to be the executable) it runs good but when i open a previously saved file to the editor it shows mysterious fonts.

like-

লিখ "The original inputs are" << নতুন_লাইন; চলবে(সংখ্যা প=০;প<যতটা;প++)

becomes

লিখ "The original inputs are" << নত�ন_লাইন; চলবে(সংখ�যা প=০;প<যতটা;প++)

one more interesting thing is that. If i type in the editor it is also working fine (no font problem).

I am using these 2 functions to read and write to file

public void writeToFile(String data,String address)
{
      try{
          // Create file 
    FileWriter fstream = new FileWriter(address);
        BufferedWriter out = new BufferedWriter(fstream);
    out.write(data);
    //Close the output stream
    out.close();
    }catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }

}


public String readFromFile(String fileName) {
   String output="";
    try {
     File file = new File(fileName);
     FileReader reader = new FileReader(file);
     BufferedReader in = new BufferedReader(reader);
     String string;
     while ((string = in.readLine()) != null) {
       output=output+string+"\n";
     }
     in.close();
   } catch (IOException e) {
     e.printStackTrace();
   }
    return output;
 }

I have set the font of the text pane to vrinda which works from within the IDE as i mentioned.

Please help me identify what is wrong.

is there something i need to do to publish JAR when native support is required?



Try changing your reading logic to use InputStreamReader which allows setting encoding:

InputStreamReader inputStreamReader = 
  new InputStreamReader(new FileInputStream (file), "UTF-8" );

Also change your writing logic to use OutputStreamWriter which allows setting encoding:

OutputStreamWriter outputStreamWriter = 
  new OutputStreamWriter(new FileOutputStream (file), "UTF-8" );


The root problem is that your current application is reading the file using the "platform default" character set / character encoding. This is obviously different when you are running from the command line and from NetBeans. In the former cause, it depends on the locale settings of the host OS or the current shell ... depending on your platform. In NetBeans, it seems to default to UTF-8.

@Andrey Adamovich's answer explains how to specify a character encoding when opening a file using a file reader or adapting a byte stream using an input stream reader.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜