简体   繁体   English

如何使用Java方法Class.getResourceAsStream()从jar中读取UTF-8文本文件?

[英]How can I read a UTF-8 text file from a jar using the Java method Class.getResourceAsStream()?

I have a UTF-8 file stored inside a java jar file. 我在Java jar文件中存储了一个UTF-8文件。 I am trying to read it using the method getResourceAsStream(), but the input stream reader that is returned by the function uses the default encoding, which is the ANSI one under Windows. 我正在尝试使用getResourceAsStream()方法读取它,但该函数返回的输入流阅读器使用默认编码,即Windows下的ANSI编码。

How can I read a UTF-8 text file from inside a jar? 如何从罐子中读取UTF-8文本文件?

Create an InputStreamReader around the InputStream , specifying UTF-8 as the encoding. 创建InputStreamReader周围InputStream ,指定UTF-8编码。

Note: I'm not sure what you mean by "the input stream reader that is returned by the function uses the default encoding" - getResourceAsStream() returns an InputStream , which reads binary data. 注意:我不确定“该函数返回的输入流阅读器使用默认编码”是什么意思getResourceAsStream()返回一个InputStream ,它读取二进制数据。 That's not a reader at all (dealing with character data). 根本不是读者 (处理字符数据)。

native2ascii may help you. native2ascii可能会帮助您。
it converts non-ascii characters to equivalent character codes inside your utf-8 file. 它将非ASCII字符转换为utf-8文件中的等效字符代码。
for example; 例如;
native2ascii -encoding Cp1254 input_file.txt output_file.properties
enjoy... 请享用...

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

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