java---Properties类的操作

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");
	}

}
java-properties-utils-1.7.1.jar是一个Java开发工具包,用于简化处理Java属性文件的操作。属性文件通常用于存储键值对,可以用于配置应用程序的参数。这个工具包提供了一些方便的方法,可以轻松地读取、写入和修改属性文件的内容。 该工具包提供了几个主要的功能。首先,它可以通过提供文件路径来加载属性文件,并将其转化为Java的属性对象。这样,我们就可以通过代码来读取和操作属性文件中的键值对。 其次,该工具包提供了一些实用的方法,用于读取和获取属性文件中的特定键值对。例如,我们可以使用getProperty()方法根据键来获取属性值,还可以使用containsKey()方法来检查属性文件中是否包含特定的键。 此外,该工具包还提供了一些方便的方法,用于向属性文件中写入和更新键值对。使用setProperty()方法可以设置属性值,而使用store()方法可以将更改后的属性文件写入到指定的文件路径中。 除了基本的读写操作,该工具包还提供了其他一些有用的功能。例如,它可以将属性对象转化为Map对象,方便我们在程序中进行进一步的处理。此外,它还支持将属性文件转化为XML格式,以便于在不同平台和环境中的数据传输和交换。 总之,java-properties-utils-1.7.1.jar是一个功能强大的Java开发工具包,可以方便地处理属性文件,并提供了许多方便的方法和功能,有助于简化我们在Java应用程序中对属性文件的操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值