简体   繁体   English

在Java中读取unicode文本并在JLabel上显示?

[英]Reading unicode text in Java and display on JLabel?

I have the following code reading a file containing unicode text (Japanese). 我有以下代码读取包含unicode文本(日语)的文件。

File f = new File("f.txt"); // the file was saved with utf-8 encoding
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);

s = br.readLine();
lblData.setText(s); // JLabel lblData was set font as Tahoma

br.close();
fr.close();

Im using window 7 and the system already installed Japanese font (MSMINCHO.TTF). 我正在使用窗口7,并且系统已经安装了日语字体(MSMINCHO.TTF)。

If I run the above code in Netbeans (6.9) editor then the program display correctly. 如果我在Netbeans(6.9)编辑器中运行以上代码,则程序将正确显示。

But when I exported to jar file and run the program independently of Netbeans, then it is no longer displaying correctly. 但是,当我导出到jar文件并独立于Netbeans运行程序时,它将不再正确显示。

I dont know why this happened and how to fix it? 我不知道为什么会这样以及如何解决?

Use FileInputStream and initialise the reader like this: 使用FileInputStream并初始化读取器,如下所示:

br = new BufferedReader(new InputStreamReader(new FileInputStream(filename), Charset.forName("UTF-8")));

that will give you the characters correct from the file. 这将为您提供文件中正确的字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM