我需要使用UTF-8在我的资源属性使用Java的ResourceBundle。当我直接在属性文件中输入文本时,它显示为mojibake。
我的应用程序运行在谷歌应用程序引擎。
谁能给我举个例子?我找不到这份工作。
我需要使用UTF-8在我的资源属性使用Java的ResourceBundle。当我直接在属性文件中输入文本时,它显示为mojibake。
我的应用程序运行在谷歌应用程序引擎。
谁能给我举个例子?我找不到这份工作。
当前回答
我尝试使用Rod提供的方法,但考虑到BalusC关注的是不要在所有应用程序中重复相同的工作,并附带了这个类:
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.ResourceBundle;
public class MyResourceBundle {
// feature variables
private ResourceBundle bundle;
private String fileEncoding;
public MyResourceBundle(Locale locale, String fileEncoding){
this.bundle = ResourceBundle.getBundle("com.app.Bundle", locale);
this.fileEncoding = fileEncoding;
}
public MyResourceBundle(Locale locale){
this(locale, "UTF-8");
}
public String getString(String key){
String value = bundle.getString(key);
try {
return new String(value.getBytes("ISO-8859-1"), fileEncoding);
} catch (UnsupportedEncodingException e) {
return value;
}
}
}
使用它的方式与常规的ResourceBundle使用非常相似:
private MyResourceBundle labels = new MyResourceBundle("es", "UTF-8");
String label = labels.getString(key)
或者你可以使用默认使用UTF-8的替代构造函数:
private MyResourceBundle labels = new MyResourceBundle("es");
其他回答
我们创建了一个资源。utf8文件,包含UTF-8格式的资源,并有一个规则运行如下:
native2ascii -encoding utf8 resources.utf8 resources.properties
Properties prop = new Properties();
String fileName = "./src/test/resources/predefined.properties";
FileInputStream inputStream = new FileInputStream(fileName);
InputStreamReader reader = new InputStreamReader(inputStream,"UTF-8");
打开设置/首选项对话框(Ctrl + Alt + S),然后单击编辑器和文件编码。
然后,在底部,您将找到属性文件的默认编码。选择您的编码类型。
或者,您可以在资源包中使用unicode符号而不是文本(例如“ів”等于\u0456\u0432)
对于当前(2021-2)Java版本,仍然有旧的ISO-8859-1函数utils.Properties#load。
如果您使用属性。装载时必须使用ISO-8859-1。 如果使用ResourceBundle,那么UTF-8应该没问题。
请允许我引用官方文件。
PropertyResourceBundle
PropertyResourceBundle can be constructed either from an InputStream or a Reader, which represents a property file. Constructing a PropertyResourceBundle instance from an InputStream requires that the input stream be encoded in UTF-8. By default, if a MalformedInputException or an UnmappableCharacterException occurs on reading the input stream, then the PropertyResourceBundle instance resets to the state before the exception, re-reads the input stream in ISO-8859-1, and continues reading. If the system property java.util.PropertyResourceBundle.encoding is set to either "ISO-8859-1" or "UTF-8", the input stream is solely read in that encoding, and throws the exception if it encounters an invalid sequence. If "ISO-8859-1" is specified, characters that cannot be represented in ISO-8859-1 encoding must be represented by Unicode Escapes as defined in section 3.3 of The Java™ Language Specification whereas the other constructor which takes a Reader does not have that limitation. Other encoding values are ignored for this system property. The system property is read and evaluated when initializing this class. Changing or removing the property has no effect after the initialization.
https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/PropertyResourceBundle.html
属性#负载
从输入字节流中读取属性列表(键和元素对)。输入流采用load(Reader)中指定的简单的面向行的格式,并假定使用ISO 8859-1字符编码;即每个字节是一个拉丁字符。非拉丁字符1和某些特殊字符使用Java™语言规范3.3节中定义的Unicode转义符在键和元素中表示。
https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Properties.html加载(java.io.InputStream)
看这个:http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html#load(java.io.Reader)
属性接受Reader对象作为参数,您可以从InputStream创建该对象。
在创建时,你可以指定Reader的编码:
InputStreamReader isr = new InputStreamReader(stream, "UTF-8");
然后将这个Reader应用到load方法:
prop.load(isr);
顺便说一句:从.properties文件中获取流:
InputStream stream = this.class.getClassLoader().getResourceAsStream("a.properties");
顺便说一句:从InputStreamReader获取资源包:
ResourceBundle rb = new PropertyResourceBundle(isr);
希望这能帮助到你!