我有一个本地化的属性文件,其中包含键值对列表。 我想将日语、中文、德语等属性值写入文件,并且还想保存带有注释和空格的现有文件布局。需要以其自己的 native 形式编写这些语言。
我尝试使用 PropertiesConfigurationLayout 将新属性(“key = akaunto ナビゲーション コンポーネント”)添加到现有属性文件中。可以以其 native 形式添加新属性,PropertiesConfigurationLayout 有助于保存文件的布局。但现有的 key 格式将更改为 unicode 格式。 有用的链接:(http://marjavamitjava.com/modifying-property-file-maintaining-order-well-comments/)
这是我尝试过的代码。
代码:
File file = new File("base_ch.properties");
PropertiesConfiguration config = new PropertiesConfiguration();
config.setEncoding("UTF-8");
PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(config);
Properties props = new Properties();
try(InputStreamReader in = new InputStreamReader(new FileInputStream(file), "UTF-8"))
{
layout.load(in);
OutputStreamWriter out =new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
props.put("key","アカウント ナビゲーション コンポーネント");
layout.save(out);
props.store(out, null);
}
catch (ConfigurationException | IOException e) {
e.printStackTrace();
}
base_ch.properties代码运行前的文件内容:
# -----------------------------------------------------------------------
# All rights reserved.
# Comments included here
# -----------------------------------------------------------------------
#Tue May 22 13:41:37
account.quote.expiration.time.label = Gültig bis
address.zipcode = 邮政编码:
base_ch.properties代码运行后文件内容:
# -----------------------------------------------------------------------
# All rights reserved.
# Comments included here
# -----------------------------------------------------------------------
#Tue May 22 13:41:37
account.quote.expiration.time.label = G\u00FCltig bis
address.zipcode = \u90AE\u653F\u7F16\u7801
#Wed Dec 06 17:40:04 IST 2017
key=アカウント ナビゲーション コンポーネント
我想保存文件布局而不进行任何更改,并且应该保留其原始形式的现有属性。 它可以使用各种类将各种语言写入属性文件,但在这种情况下文件布局将会发生变化。 PropertiesConfigurationLayout 是我发现保存布局的唯一方法。
有人可以帮助我吗?
最佳答案
找到了问题的解决方案。
自定义 PropertiesConfiguration 和 PropertiesConfigurationLayout 类以满足要求。
重写 PropertiesConfiguration 中的 escapeValue() 方法,以返回不带转义字符的属性值本身。
重写 PropertiesConfigurationLayout 中的save方法。
下面给出的是扩展 PropertiesConfiguration 的类:
public class PropertiesConfigurationExtended extends PropertiesConfiguration{
private static final char[] SEPARATORS = new char[] {'=', ':'};
private static final char[] WHITE_SPACE = new char[]{' ', '\t', '\f'};
private static final String ESCAPE = "\\";
public static class PropertiesWriter extends PropertiesConfiguration.PropertiesWriter{
private char delimiter;
/**
* Constructor.
*/
public PropertiesWriter(Writer writer, char delimiter)
{
super(writer,delimiter);
this.delimiter = delimiter;
}
public void writeProperty(String key, Object value,
boolean forceSingleLine) throws IOException
{
String v;
if (value instanceof List)
{
List values = (List) value;
if (forceSingleLine)
{
v = makeSingleLineValue(values);
}
else
{
writeProperty(key, values);
return;
}
}
else
{
v = escapeValue(value);
}
write(escapeKey(key));
write(" = ");
write(v);
writeln(null);
}
/**
* Rewrite the escapeValue method to avoid escaping of the given property value.
*
* @param value the property value
* @return the same property value
*/
private String escapeValue(Object value)
{
return String.valueOf(value);
}
}
}
下面给出的是扩展 PropertiesConfigurationLayout 的类:
public class PropertiesConfigurationLayoutExtended extends PropertiesConfigurationLayout{
public PropertiesConfigurationLayoutExtended(PropertiesConfigurationExtended config) {
super(config);
}
public void save(Writer out) throws ConfigurationException
{
try
{
char delimiter = getConfiguration().isDelimiterParsingDisabled() ? 0
: getConfiguration().getListDelimiter();
PropertiesConfigurationExtended.PropertiesWriter writer = new PropertiesConfigurationExtended.PropertiesWriter(
out, delimiter);
if (getHeaderComment() != null)
{
writer.writeln(getCanonicalHeaderComment(true));
writer.writeln(null);
}
for (Iterator it = getKeys().iterator(); it.hasNext();)
{
String key = (String) it.next();
if (getConfiguration().containsKey(key))
{
// Output blank lines before property
for (int i = 0; i < getBlancLinesBefore(key); i++)
{
writer.writeln(null);
}
// Output the comment
if (getComment(key) != null)
{
writer.writeln(getCanonicalComment(key, true));
}
// Output the property and its value
boolean singleLine = (isForceSingleLine() || isSingleLine(key))
&& !getConfiguration().isDelimiterParsingDisabled();
writer.writeProperty(key, getConfiguration().getProperty(
key), singleLine);
}
}
writer.flush();
}
catch (IOException ioex)
{
throw new ConfigurationException(ioex);
}
}
}
用于将属性写入文件的类:
File file = new File("base_ch.properties");
PropertiesConfigurationExtended config = new PropertiesConfigurationExtended();
PropertiesConfigurationLayoutExtended layout = new PropertiesConfigurationLayoutExtended(config);
try(InputStreamReader in = new InputStreamReader(new FileInputStream(file), "UTF-8"))
{
layout.load(in);
OutputStreamWriter out =new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
config.setProperty("key","アカウント ナビゲーション コンポーネント");
layout.save(out, false));
}
catch (ConfigurationException | IOException e) {
e.printStackTrace();
}
关于java - 如何通过保存文件布局将不同语言的属性值以其 native 形式写入java中的属性文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47674469/