您好,欢迎来到欧得旅游网。
搜索
您的当前位置:首页Properties的读写

Properties的读写

来源:欧得旅游网
package knowledgepoint;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
//properties:读写资源配置文件,是HashTable的子类。Properties的键和值只能是String
/*
 * 方法:setProperty(String key,String value)
 * 	getProperty(String key) get Property(String key,String defaultValue)(未找到值时的默认值)
 * 资源配置文件后缀.properties
 *  		写:	store(OutputStream out, String comments) 
				store(Writer writer, String comments) 
			读:   load(InputStream inStream) 
			    load(Reader reader) 
			后缀.xml
			写:storeToXML(OutputStream os, String comment):UTF-8字符集
			   storeToXML(OutputStream os, String comment, String encoding) 
			读:loadFromXML(InputStream in) 
 */
public class PropertiesDemol {
	public static void main(String[] args) throws FileNotFoundException, IOException {
		Properties properties = new Properties();
		/*properties.setProperty("资源配置","1");
		properties.setProperty("资源配置","2");
		System.out.println(properties.size());
		System.out.println(properties.getProperty("资源配置"));
		System.out.println(properties.getProperty("资源配置文件","未找到时的默认值"));*/
		properties.setProperty("first","第一");
		properties.setProperty("second", "第二");
		properties.setProperty("Third", "第三");
		//相对路径,相对当前工程
		//properties.store(new FileOutputStream("p1.properties"),"后缀为.properties的读写");
		//properties.storeToXML(new FileOutputStream("p1.xml"),"后缀为.xml的资源配置文件");
		//读取相对路径
		/*Properties pro = new Properties();
		pro.load(new FileReader("p1.properties"));
		System.out.println(properties.getProperty("first"));*/
		//类相对路径
		//1.  /代表bin
		/*Properties pro = new Properties();
		pro.load(PropertiesDemol.class.getResourceAsStream("/p1.properties"));
		System.out.println(pro.getProperty("second"));*/
		//2.""代表bin目录
		Properties pro = new Properties();
		pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("p1.properties"));
		System.out.println(pro.getProperty("Third"));
	}
}

 

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- ovod.cn 版权所有 湘ICP备2023023988号-4

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务