简体   繁体   English

在属性文件中保存特殊字符

[英]Saving special characters in properties file

I have property with string that contains some special characters. 我有字符串属性包含一些特殊字符。 After I save it to properties file I have: 将它保存到属性文件后,我有:

BB\u0161BB=0

I don't like character represented with . 我不喜欢用表示的字符。 Why it can't save like character I see it on the screen and type from keyboard? 为什么它不能像角色一样保存我在屏幕上看到它并从键盘输入?

UPD What is the easiest way to read ini-file architecture file that contains special character? UPD读取包含特殊字符的ini文件架构文件的最简单方法是什么?

That's how Properties files are defined to behave. 这就是定义属性文件的行为方式。 Any other system is likely to use UTF-8 which might not be readable either. 任何其他系统都可能使用UTF-8,也可能无法读取。

As your character is outside the range of an ISO-8859-1 encoding, it has to use unicode instead. 由于您的角色超出了ISO-8859-1编码的范围,因此必须使用unicode。

You can't. 你不能。 From javadoc: 来自javadoc:

...the input/output stream is encoded in ISO 8859-1 character encoding. ...输入/输出流以ISO 8859-1字符编码进行编码。 Characters that cannot be directly represented in this encoding can be written using Unicode escapes as defined in section 3.3 of The Java™ Language Specification; 无法在此编码中直接表示的字符可以使用“Java™语言规范”第3.3节中定义的Unicode转义编写; only a single 'u' character is allowed in an escape sequence. 在转义序列中只允许一个'u'字符。

Please refer to this How to use UTF-8 in resource properties with ResourceBundle 请参阅此如何在ResourceBundle的资源属性中使用UTF-8

属性文件是ISO-8859-1编码的,因此该集合之外的字符需要进行\\uXXXX转义。

You can! 您可以!

Encode your file in unicode (UTF-8, for instance) using another application (Notepad++ is nice, for instance) and read your properties file like this: 使用其他应用程序(例如,Notepad ++很好)以unicode(例如UTF-8)对文件进行编码,并读取您的属性文件,如下所示:

File file = ... ;
Properties properties = new Properties();
try (FileInputStream in = new FileInputStream(file);
     Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
  properties.load(reader);
}
// use properties

There you go, properties file in any charset that you can read and use. 你去,你可以阅读和使用的任何字符集中的属性文件。

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

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