Java Properties是否已被有效弃用? [英] Are Java Properties effectively deprecated?

查看:238
本文介绍了Java Properties是否已被有效弃用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java的属性对象没有自Java 5之前发生了很大变化,它没有Generics支持,或者没有非常有用的辅助方法(定义模式来插入类来处理属性或帮助加载目录中的所有属性文件)。

Java's Properties object hasn't changed much since pre-Java 5, and it hasn't got Generics support, or very useful helper methods (defined pattern to plug in classes to process properties or help to load all properties files in a directory, for example).

属性的开发是否已停止?如果是这样,目前保存/加载此类属性的最佳做法是什么?

Has development of Properties stopped? If so, what's the current best practice for this kind of properties saving/loading?

或者我完全错过了什么?

Or have I completely missed something?

推荐答案

围绕属性的许多概念肯定是古老而有问题的。它具有非常差的国际化,它增加了今天只能通过Generic类型实现的方法,它扩展了Hashtable,它本身通常是不可用的,因为它的同步价值有限,而且它的方法与它不一致。 1.2中引入的集合类,添加到Properties类的许多方法实质上提供了由泛型替换的类型安全性。

A lot of the concepts around Properties are definitely ancient and questionable. It has very poor internationalization, it adds methods that today would just be accomplished via a Generic type, it extends Hashtable, which is itself generally out of use, since its synchronization is of limited value and it has methods which are not in harmony with the Collections classes introduced in 1.2, and many of the methods added to the Properties class essentially provide the kind of type safety that is replaced by Generics.

如果今天实现它可能会是 Map< String,String> 的特殊实现,当然支持在属性文件中更好的编码。

If implemented today it would probably be a special implementation of a Map<String, String>, and certainly support better encoding in the properties file.

话虽如此,但实际上没有一个替代品不会增加复杂性。当然java.util.prefs.Preferences api是新的和改进的,但它增加了一层复杂性,远远超出了许多用例所需的复杂性。仅使用XML也是一种选择(至少可以解决国际化问题),但是属性对象通常可以很好地满足需求,此时可以使用它。

That being said, there isn't really a replacement that doesn't add complexity. Sure the java.util.prefs.Preferences api is the "new and improved" but it adds a layer of complexity that is well beyond what is needed for many use cases. Just using XML is also an option (which at least fixes the internationalization issues) but a properties object often fits the needs just fine, at which point use it.

这篇关于Java Properties是否已被有效弃用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆