java properties类的作用
Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。像Python支持的配置文件是.ini文件,同样,它也有自己读取配置文件的类ConfigParse,方便程序员或用户通过该类的方法来修改.ini配置文件。在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用"#"来注释。
Properties类的方法
这里提供几个主要的方法
-
String getProperty(String key)
Searches for the property with the specified key in this property list. -
void list(PrintStream out)
Prints this property list out to the specified output stream. -
void list(PrintWriter out)
Prints this property list out to the specified output stream. -
void load(InputStream inStream)
Reads a property list (key and element pairs) from the input character stream in a simple line-oriented format. -
void loadFromXML(InputStream in)
Loads all of the properties represented by the XML document on the specified input stream into this properties table. -
Object setProperty(String key, String value)
Calls the Hashtable method put. -
void store(OutputStream out, String comments)
Writes this property list (key and element pairs) in this Properties table to the output stream in a format suitable for loading into a Properties table using the load(InputStream) method. -
void store(Writer writer, String comments)
Writes this property list (key and element pairs) in this Properties table to the output character stream in a format suitable for using the load(Reader) method.
- void storeToXML(OutputStream os, String comment)
Emits an XML document representing all of the properties contained in this table.
- void storeToXML(OutputStream os, String comment, String encoding)
Emits an XML document representing all of the properties contained in this table, using the specified encoding.
- Set < String > stringPropertyNames()
Returns a set of keys in this property list where the key and its corresponding value are strings, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list.
- Enumeration<?> propertyNames()
Returns an enumeration of all the keys in this property list, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list.
java 读取Properties文件的方法
java读取properties文件的方法有很多。常用的有两种:
- InputStream in = getClass().getResourceAsStream(“资源Name”);
- InputStream in = new BufferedInputStream(new FileInputStream(filepath));
相关实例
- 获取JVM系统属性
public static void main(String[] args) {
Properties pps = System.getProperties();
pps.list(System.out);
}
- 新建一个properties文件
public static void main(String[] args) throws IOException {
FileInputStream fin = new FileInputStream("H:\\Test\\Test\\src\\com\\xzy\\Demo1\\test.properties");
Properties pps = new Properties();
pps.load(fin);
Enumeration enum1 = pps.propertyNames();
while(enum1.hasMoreElements())
{
String strkey = (String)enum1.nextElement();
String strvalue = pps.getProperty(strkey);
System.out.println(strkey+"="+strvalue);
}
}
public class Study {
//根据key读取value
public static String getValue(String filePath,String key) throws IOException
{
Properties pps = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);
String value = pps.getProperty(key);
System.out.println(key+"--"+value);
return value;
}
//读取properties的全部信息
public static void getAllInformation(String filepath) throws IOException
{
Properties pps = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(filepath));
pps.load(in);
pps.list(System.out);
}
//写入properties信息
public static void writeProperties(String filepath,String key,String value) throws IOException
{
Properties pps = new Properties();
InputStream in = new FileInputStream(filepath);
//从输入流中读取属性列表(键和元素对)
pps.load(in);
//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream out = new FileOutputStream(filepath);
pps.setProperty(key, value);
//以适合使用 load 方法加载到 Properties 表中的格式,
//将此 Properties 表中的属性列表(键和元素对)写入输出流
pps.store(out, "Update " + key + " name");
}
public static void main(String[] args) throws IOException {
writeProperties("H:\\Test\\Test\\src\\com\\xzy\\Demo1\\test.properties", "123", "456");
}
}