native2ascii工具已经在java8以后的版本移除了:
Removed native2ascii Tool
The native2ascii
tool has been removed from the JDK. Because JDK 9 and later releases support UTF-8 based properties resource bundles, the conversion tool for UTF-8 based properties resource bundles to ISO-8859-1 is no longer needed.
See UTF-8 Properties Files in Java Platform, Standard Edition Internationalization Guide.
来源:https://docs.oracle.com/javase/10/migrate/toc.htm#JSMIG-GUID-A3F99AB7-7425-4420-8EDE-E4237BC5FD7D
因为java8以后的jdk支持utf-8编码的properties文件,不需要再将非ascii编码字符转成unicode字符编码,所以native2ascii工具没用了:
UTF-8 Properties Files
In Java SE 9, properties files are loaded in UTF-8 encoding. In previous releases, ISO-8859-1 encoding was used for loading property resource bundles. UTF-8 is a much more convenient way to represent non-Latin characters.
Most existing properties files should not be affected: UTF-8 and ISO-8859-1 have the same encoding for ASCII characters, and human-readable non-ASCII ISO-8859-1 encoding is not valid UTF-8. If an invalid UTF-8 byte sequence is detected, the Java runtime automatically rereads the file in ISO-8859-1.
If there is an issue, consider the following options:
-
Convert the properties file into UTF-8 encoding.
- Specify the runtime system property for the properties file's encoding, as in this example:
java.util.PropertyResourceBundle.encoding=ISO-8859-1
See java.util.PropertyResourceBundle.
实际试了一下,可以在项目中resource文件夹中直接新建properties文件,如果包含汉字等字符的话,会提示你将properties文件另存为utf-8编码的文件,然后可以正常输出。
但是如果使用记事本建立properties文件,然后保存为utf-8格式,再复制到resource文件夹下,输出就会报错,不知道什么原因,难道只能在eclipse中新建properties文件?
一个小例子:
import java.util.Locale;
import java.util.ResourceBundle;
public class Demo3 {
public static void main(String[] args) {
Locale myLocale = Locale.getDefault();
System.out.println(myLocale); //zh_CN
// myLocale = new Locale("en", "US");
// 根据指定语言_国家环境加载资源文件
ResourceBundle bundle = ResourceBundle.getBundle("msg", myLocale);
// System.out.println(java.util.PropertyResourceBundle.encoding);
// 从资源文件中取得的消息
System.out.println(bundle.getString("name"));
// System.out.println(bundle.getString("time"));
}
}
import java.util.Locale;
public class LocaleTest {
public static void main(String[] args) {
// 返回Java所支持的全部国家和语言的数组
Locale[] localeList = Locale.getAvailableLocales();
for (Locale locale : localeList)
{
System.out.println(locale.getLanguage() + "_" + locale.getCountry());
System.out.println(locale.getDisplayLanguage() + "_" + locale.getDisplayCountry());
}
System.out.println("=========================");
Locale myLocale = Locale.getDefault();
System.out.println(myLocale); //zh_CN
System.out.println(Locale.CHINA); //zh_CN
myLocale = new Locale("en", "US"); //语言 国家, 强制换成en_US
System.out.println(myLocale); //en_US
}
}
properties文件的寻找顺序(假设当前locale为en_US):
msg_en_US.properties
msg_en.properties
msg_zh_CN.properties
msg_zh.properties
msg.properties
会先找当前locale,没有的话再找本机默认的