Java Native language Application Doesnt work outside IDE

前端 未结 2 1818
臣服心动
臣服心动 2021-01-27 14:02

I have a program developed under java with netbeans. It has a text pane that takes text written in non English language and do some operation including save open new.....

<
相关标签:
2条回答
  • 2021-01-27 14:39

    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" );
    
    0 讨论(0)
  • 2021-01-27 14:41

    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 讨论(0)
提交回复
热议问题