如何在Jar文件中获取Unicode [英] How to get Unicode in Jar files
问题描述
我写了一个从程序外部调用Unicode的程序。我正在使用Windows XP和Eclipse。当我在IDE中运行该程序时,它会显示Unicode,但当我将其导出为.jar文件时,我无法读取Unicode并显示为框。
I wrote a program for calling Unicode from outside the program. I am using Windows XP and Eclipse. When I run that program in the IDE, it shows the Unicode, but when I exported it as a .jar file I am unable read the Unicode and it shows as boxes.
我按照这些说明在我的计算机上安装Unicode。我按照链接将泰卢固语字体安装到我的系统。
I followed these instructions to install Unicode in my computer. I followed links to install the Telugu fonts to my system.
任何人都可以告诉我如何在Jar文件中获取Unicode? p>
Can any one please tell me how can I get Unicode in Jar files?
推荐答案
虽然没有直接回答这个问题,但这里有一个关于如何从文本文件中正确读/写的小方法,以OS依赖的方式。
While not answering the question directly, here is a small howto on how to read/write correctly from text files, in an OS dependent way.
首先要知道的是JVM有一个 file.encoding
属性。它定义了用于所有文件读/写操作的默认编码,所有读取器在未指定编码时使用。
First thing to know is that the JVM has a file.encoding
property. It defines the default encoding used for all file read/write operation, all readers used when not specifying an encoding.
因此,您不希望使用默认编码构造函数,但每次都定义编码。在Java中,体现编码的类是 Charset
。如果你想要UTF-8,你将使用:
As such, you don't want to use the default constructors, but define the encoding each time. In Java, the class which "embodies" an encoding is Charset
. If you want UTF-8, you will use:
-
StandardCharsets.UTF_8
( Java 7 +), -
Charset.forName(UTF-8)
(Java 6 - ), -
Charsets.UTF_8
(如果您使用Guava)。
StandardCharsets.UTF_8
(Java 7+),Charset.forName("UTF-8")
(Java 6-),Charsets.UTF_8
(if you use Guava).
为了正确读取文件,打开 InputStream
到该文件,然后打开 InputStreamReader
c $ c> InputStream (在下面的代码示例中, UTF8
是从上述方法之一获得的UTF-8字符集):
In order to read a file correctly, open an InputStream
to that file, then an InputStreamReader
over that InputStream
(in the code samples below, UTF8
is the UTF-8 charset obtained from one of the methods above):
final InputStream in = new FileInputStream(...);
final Reader reader = new InputStreamReader(in, UTF8);
为了正确写入文件,请打开 OutputStream
到它,然后 OutputStreamWriter
超过 OutputStream
:
In order to write a file correctly, open an OutputStream
to it, then an OutputStreamWriter
over that OutputStream
:
final OutputStream out = new FileOutputStream(...);
final Writer writer = new OutputStreamWriter(out, UTF8);
当然,不要忘记 .close()
最后块中的流/读者/编写者的。提示:如果您不使用Java 7,请使用Guava 14.0+,使用 更接近
。这是处理多个I / O资源并确保正确处理它们的最安全方式。
And, of course, do not forget to .close()
both of the streams/readers/writers in a finally
block. Hint: if you don't use Java 7, use Guava 14.0+, use Closer
. It is the most secure way to deal with multiple I/O resources and ensuring they are dealt with correctly.
这篇关于如何在Jar文件中获取Unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!