swing Java本机语言应用程序无法在IDE之外工作
我有一个用netbeans在java下开发的程序。它有一个文本窗格,可以获取用非英语编写的文本,并执行一些操作,包括“保存”“打开”“新建…”
当我从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吗
# 1 楼答案
尝试更改阅读逻辑,以使用允许设置编码的InputStreamReader:
还可以将写入逻辑更改为使用OutputStreamWriter,它允许设置编码:
# 2 楼答案
根本问题是,当前应用程序正在使用“平台默认”字符集/字符编码读取文件。当您从命令行和NetBeans运行时,这显然是不同的。在前一个原因中,它取决于主机操作系统或当前shell的区域设置。。。取决于你的平台。在NetBeans中,它似乎默认为UTF-8
@Andrey Adamovich的回答解释了如何在使用文件读取器打开文件或使用输入流读取器调整字节流时指定字符编码