如何按定义的顺序编写Java属性?
我使用java.util.Properties的store(Writer,String)方法来存储属性。在生成的文本文件中,属性以随机顺序存储 这就是我正在做的:如何按定义的顺序编写Java属性?,java,properties-file,Java,Properties File,我使用java.util.Properties的store(Writer,String)方法来存储属性。在生成的文本文件中,属性以随机顺序存储 这就是我正在做的: Properties properties = createProperties(); properties.store(new FileWriter(file), null); 如何确保按字母顺序或添加属性的顺序写入属性 我希望有一个比“手动创建属性文件”更简单的解决方案。根据“新白痴”的建议,它按字母键顺序存储 Properti
Properties properties = createProperties();
properties.store(new FileWriter(file), null);
如何确保按字母顺序或添加属性的顺序写入属性
我希望有一个比“手动创建属性文件”更简单的解决方案。根据“新白痴”的建议,它按字母键顺序存储
Properties tmp = new Properties() {
@Override
public synchronized Enumeration<Object> keys() {
return Collections.enumeration(new TreeSet<Object>(super.keySet()));
}
};
tmp.putAll(properties);
tmp.store(new FileWriter(file), null);
Properties tmp=新属性(){
@凌驾
公共同步枚举密钥(){
返回Collections.enumeration(新树集(super.keySet());
}
};
tmp.putAll(物业);
store(新的FileWriter(文件),null);
有关允许按明确定义的顺序读取/写入属性文件的完整实现,请参阅
OrderedProperties properties = new OrderedProperties();
properties.load(new FileInputStream(new File("~/some.properties")));
Steve McLeod的解决方案在尝试区分大小写排序时不起作用 这就是我想到的
Properties newProperties = new Properties() {
private static final long serialVersionUID = 4112578634029874840L;
@Override
public synchronized Enumeration<Object> keys() {
Comparator<Object> byCaseInsensitiveString = Comparator.comparing(Object::toString,
String.CASE_INSENSITIVE_ORDER);
Supplier<TreeSet<Object>> supplier = () -> new TreeSet<>(byCaseInsensitiveString);
TreeSet<Object> sortedSet = super.keySet().stream()
.collect(Collectors.toCollection(supplier));
return Collections.enumeration(sortedSet);
}
};
// propertyMap is a simple LinkedHashMap<String,String>
newProperties.putAll(propertyMap);
File file = new File(filepath);
try (FileOutputStream fileOutputStream = new FileOutputStream(file, false)) {
newProperties.store(fileOutputStream, null);
}
Properties newProperties=newProperties(){
私有静态最终长serialVersionUID=4112578634029874840L;
@凌驾
公共同步枚举密钥(){
Comparator byCaseInsensitiveString=Comparator.comparing(Object::toString,
字符串。不区分大小写(顺序);
供应商=()->新树集(按案例不敏感预算);
TreeSet sortedSet=super.keySet().stream()
.收集(收集器.收集(供应商));
返回集合。枚举(sortedSet);
}
};
//propertyMap是一个简单的LinkedHashMap
newProperties.putAll(propertyMap);
文件文件=新文件(文件路径);
try(FileOutputStream FileOutputStream=newfileoutputstream(file,false)){
store(fileOutputStream,null);
}
使用树集
是危险的!
因为在不区分大小写的顺序中
字符串“mykey”、“mykey”和“mykey”将产生相同的索引!(因此将省略2个键)
我使用列表
,以确保保留所有钥匙
List<Object> list = new ArrayList<>( super.keySet());
Comparator<Object> comparator = Comparator.comparing( Object::toString, String.CASE_INSENSITIVE_ORDER );
Collections.sort( list, comparator );
return Collections.enumeration( list );
List List=newarraylist(super.keySet());
Comparator Comparator=Comparator.Comparating(对象::toString,String.CASE\u不区分大小写\u顺序);
集合。排序(列表、比较);
返回集合。枚举(列表);
Steve McLeod的答案过去对我有用,但自从Java 11以来,就没有了
问题似乎出在入口集排序上,因此,现在开始:
@SuppressWarnings("serial")
private static Properties newOrderedProperties()
{
return new Properties() {
@Override public synchronized Set<Map.Entry<Object, Object>> entrySet() {
return Collections.synchronizedSet(
super.entrySet()
.stream()
.sorted(Comparator.comparing(e -> e.getKey().toString()))
.collect(Collectors.toCollection(LinkedHashSet::new)));
}
};
}
@SuppressWarnings(“串行”)
私有静态属性newOrderedProperties()
{
返回新属性(){
@重写公共同步集入口集(){
return Collections.synchronizedSet(
super.entrySet()
.stream()
.sorted(Comparator.comparing(e->e.getKey().toString()))
.collect(Collectors.toCollection(LinkedHashSet::new));
}
};
}
我要警告你,这无论如何都不快。它强制在LinkedHashSet上进行迭代,这并不理想,但我愿意接受建议。这有帮助吗?只需覆盖
公共同步枚举键()
,而不是键集()
。这将不包括插入顺序的情况。fge您是正确的。但是作为OP,我对字母顺序有点满意。它会先对大写字母排序,然后再对小写字母排序。我更喜欢Etienne Studer的答案,因为如果你想有插入顺序,你不能通过这种方式实现,因为它在内部已经使用了哈希表。在这种情况下,LinkedHashTable或LinkedHashSet不会有任何效果。我认为这仍然不起作用?keys()
中的断点不会触发。我通过重写entrySet()
方法解决了这个问题。使用super.entrySet()
,转换为列表,对列表排序,转换回集合并返回。您是否检查了文件中存储的内容?我有一种感觉,您用于自定义属性的映射根本没有连接到文件,您在文件中存储了默认哈希表,如果我错了,很抱歉。这是测试范围:是的,我明白了,谢谢你的回答,我试图在安卓中使用它,但实际上java属性在安卓中有不同的实现,因此,您还需要重写entrySet()方法才能使其正常工作。对于那些没有在github上看到链接的人,这里提供了maven工件:keys()
方法的第2行和第3行可以替换为:Set sortedKeys=new TreeSet(byCaseInsensitiveString)代码>后跟:sortedKeys.addAll(super.keySet())代码>