Java 本机语言应用程序在 IDE 之外无法运行

发布于 2024-11-02 16:39:04 字数 1411 浏览 1 评论 0原文

我有一个在java下用netbeans开发的程序。它有一个文本窗格,可以获取用非英语编写的文本,并执行一些操作,包括保存、打开新的……

当我从 netbeans 运行该程序时,该程序运行良好且完整,工作完美。但是,当我转到 dist 文件夹并运行 jar(应该是可执行文件)时,它运行良好,但是当我在编辑器中打开以前保存的文件时,它会显示神秘的字体。

就像

-লিখ“原始输入是”<< নতুন_লাইন; চলবে(সংখ্যা প=০;প<যতটা;প++)

变为

àпайская–“原始输入是” << à à ¤ à § à à à ́à ́ ́à ́ à ̀; à¸à¤à¤à§‡(à¸à¤à¤à¤à¤à¤àª=৪;àª<à¸à¤à¤à¤ ¤;àª++)

还有一件更有趣的事情是。如果我在编辑器中输入,它也可以正常工作(没有字体问题)。

我正在使用这两个函数来读取和写入文件,

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;
 }

我已将文本窗格的字体设置为 vrinda,正如我提到的,它在 IDE 中工作。

请帮我找出问题所在。

当需要本机支持时,我需要做什么来发布 JAR?


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.....

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?


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

童话里做英雄 2024-11-09 16:39:04

尝试更改您的读取逻辑以使用允许设置编码的InputStreamReader:

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

同时更改您的写入逻辑以使用允许设置编码的OutputStreamWriter:

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

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" );
红尘作伴 2024-11-09 16:39:04

根本问题是您当前的应用程序正在使用“平台默认”字符集/字符编码读取文件。当您从命令行和 NetBeans 运行时,这显然是不同的。在前一个原因中,它取决于主机操作系统或当前 shell 的区域设置...取决于您的平台。在 NetBeans 中,似乎默认为 UTF-8。

@Andrey Adamovich 的答案解释了如何在使用文件读取器打开文件或使用输入流读取器调整字节流时指定字符编码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文