📌  相关文章
📜  Java中的properties类

📅  最后修改于: 2020-10-13 00:42:46             🧑  作者: Mango

Java中的属性类

属性对象包含作为字符串的键和值对。 java.util.Properties类是Hashtable的子类。

它可以用于基于属性键获取属性值。 Properties类提供了从属性文件中获取数据并将数据存储到属性文件中的方法。而且,它可以用来获取系统的属性。

属性文件的优点

如果从属性文件中更改了信息,则不需要重新编译:如果从属性文件中更改了任何信息,则无需重新编译java类。它用于存储经常更改的信息。

属性类的构造方法

Method Description
Properties() It creates an empty property list with no default values.
Properties(Properties defaults) It creates an empty property list with the specified defaults.

属性类的方法

下面给出了Properties类的常用方法。

Method Description
public void load(Reader r) It loads data from the Reader object.
public void load(InputStream is) It loads data from the InputStream object
public void loadFromXML(InputStream in) It is used to load all of the properties represented by the XML document on the specified input stream into this properties table.
public String getProperty(String key) It returns value based on the key.
public String getProperty(String key, String defaultValue) It searches for the property with the specified key.
public void setProperty(String key, String value) It calls the put method of Hashtable.
public void list(PrintStream out) It is used to print the property list out to the specified output stream.
public void list(PrintWriter out)) It is used to print the property list out to the specified output stream.
public Enumeration propertyNames()) It returns an enumeration of all the keys from the property list.
public Set stringPropertyNames() It returns a set of keys in from property list where the key and its corresponding value are strings.
public void store(Writer w, String comment) It writes the properties in the writer object.
public void store(OutputStream os, String comment) It writes the properties in the OutputStream object.
public void storeToXML(OutputStream os, String comment) It writes the properties in the writer object for generating XML document.
public void storeToXML(Writer w, String comment, String encoding) It writes the properties in the writer object for generating XML document with the specified encoding.

从属性文件获取信息的Properties类示例

要从属性文件中获取信息,请首先创建属性文件。

user=system
password=oracle

现在,让我们创建java类以从属性文件中读取数据。

import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args)throws Exception{
FileReader reader=new FileReader("db.properties");

Properties p=new Properties();
p.load(reader);

System.out.println(p.getProperty("user"));
System.out.println(p.getProperty("password"));
}
}
Output:system
       oracle

现在,如果您更改属性文件的值,则无需重新编译java类。这意味着没有维护问题。

获取所有系统属性的Properties类示例

通过System.getProperties()方法,我们可以获得系统的所有属性。让我们创建一个从系统属性获取信息的类。

import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args)throws Exception{

Properties p=System.getProperties();
Set set=p.entrySet();

Iterator itr=set.iterator();
while(itr.hasNext()){
Map.Entry entry=(Map.Entry)itr.next();
System.out.println(entry.getKey()+" = "+entry.getValue());
}

}
}
Output:
java.runtime.name = Java(TM) SE Runtime Environment
sun.boot.library.path = C:\Program Files\Java\jdk1.7.0_01\jre\bin
java.vm.version = 21.1-b02
java.vm.vendor = Oracle Corporation
java.vendor.url = http://java.oracle.com/
path.separator = ;
java.vm.name = Java HotSpot(TM) Client VM
file.encoding.pkg = sun.io
user.country = US
user.script = 
sun.java.launcher = SUN_STANDARD
...........

用于创建属性文件的Properties类的示例

现在,让我们编写代码以创建属性文件。

import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args)throws Exception{

Properties p=new Properties();
p.setProperty("name","Sonoo Jaiswal");
p.setProperty("email","sonoojaiswal@javatpoint.com");

p.store(new FileWriter("info.properties"),"Javatpoint Properties Example");

}
}

让我们看看生成的属性文件。

#Javatpoint Properties Example
#Thu Oct 03 22:35:53 IST 2013
email=sonoojaiswal@javatpoint.com
name=Sonoo Jaiswal