加载跟写入properties属性文件的工具类

加载和写入properties属性文件的工具类

很全面的工具类PropertiesTool,主要功能如下:

1,可以文件路径、字节流、字符流和编码等加载内容,

如loadFile(String filename,String encoding),loadStream(InputStream stream,String encoding)和loadReader(Reader reader)

2,在获取value时,可以主动将数据进行判断和类型转换,

如getStrings(String key,String regex),getCharacter(String key),getBoolean(String key)getInteger(String key)等

3,是否包含键和值,

如containsKey(String key)和containsValue(String value)

4,可以更改属性值,

如setValue(String key,String value)

5,可以获取key集合和键值对的Entry集合

如,getKeySet()和getEntrySet()

6,可以将已经加载和改变的所有东西,根据自定的字符编码,写入文件、字符流和字节流中,

如,writeToFile(String filename),writeToFile(String filename,String encoding),writeToStream(OutputStream stream,String encoding),writeToStream(OutputStream stream),writeToWriter(Writer writer)

 

工具类具体代码如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

/** 
 * @author: 			forestqqqq@163.com
 * @class:			PropertiesTool
 * @package:		test5
 * @description: 	加载属性文件中属性的工具类 ,应该是线程安全的
 */
public class PropertiesTool {
	private Properties tool;
	public PropertiesTool(){
		tool = new Properties();
	}
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			loadFile
	 * @description:	加载属性文件中的内容
	 * @param filename	属性文件路径
	 * @param encoding	文件的编码
	 * @return			是否加载成功,文件路径是否正确 
	 */
	public boolean loadFile(String filename,String encoding){
		try {
			FileInputStream in = new FileInputStream(filename);
			return loadStream(in,encoding);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			loadStream
	 * @description:	读取字节流加载内容
	 * @param stream	要读取的字节流
	 * @param encoding	字符流编码
	 * @return
	 */
	public boolean loadStream(InputStream stream,String encoding){
		try {
			Reader reader = new InputStreamReader(stream,encoding);
			return loadReader(reader);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			loadReader
	 * @description:	读取字符流加载内容
	 * @param reader	要读取的字符流
	 * @return
	 */
	public synchronized boolean loadReader(Reader reader){
		try {
			tool.load(reader);
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			getTool
	 * @description:	获取Properties类型加载器
	 * @return
	 */
	public synchronized Properties getTool(){
		return tool;
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			getString
	 * @description:	加载一个字符串。如果没有对应的key,就返回null
	 * @param key		键值对中的key
	 * @return			键值对中的value
	 */
	public String getString(String key){
		return getTool().getProperty(key);
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			getString
	 * @description:	加载一个字符串。如果没有对应的key,就返回默认值
	 * @param key		键值对中的key
	 * @param defaultValue	默认值
	 * @return			键值对中的value
	 */
	public String getString(String key,String defaultValue){
		return getTool().getProperty(key, defaultValue);
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			getStrings
	 * @description:	加载一个String数组,并且以regex正则表达式分割该字符串。如果没有对应的key则返回null
	 * @param key		键值对中的key
	 * @param regex		正则表达式
	 * @return
	 */
	public String [] getStrings(String key,String regex){
		String str = getTool().getProperty(key);
		if(str == null){
			return null;
		}
		return str.split(regex);
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			getCharacter
	 * @description:	加载一个Character。如果没有对应的key或者value的长度不是1,则返回null
	 * @param key
	 * @return
	 */
	public Character getCharacter(String key){
		String str = getTool().getProperty(key);
		if(str == null || str.length() != 1){
			return null;
		}
		return str.charAt(0);
	}
	
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			getBoolean
	 * @description:	加载一个Boolean。如果没有对应的key,则返回null;如果值是"true"或"1",返回true,否则返回false
	 * @param key
	 * @return
	 */
	public Boolean getBoolean(String key){
		String str = getTool().getProperty(key);
		if(str == null){
			return null;
		}
		//这里比较"true"时,使用了不区分大小写的比较
		if("true".equalsIgnoreCase(str) || "1".equals(str)){
			return true;
		}
		return false;
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			getInteger
	 * @description:	加载一个Integer。如果没有对应的key或者value无法转换成为Integer,则返回null
	 * @param key
	 * @return
	 */
	public Integer getInteger(String key){
		String str = getTool().getProperty(key);
		if(str == null){
			return null;
		}
		Integer value = null;
		try {
			value = Integer.parseInt(str);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return value;
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			getLong
	 * @description:	加载一个Long。如果没有对应的key或者value无法转换成为Long,则返回null
	 * @param key
	 * @return
	 */
	public Long getLong(String key){
		String str = getTool().getProperty(key);
		if(str == null){
			return null;
		}
		Long value = null;
		try {
			value = Long.parseLong(str);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return value;
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			getShort
	 * @description:	加载一个Short。如果没有对应的key或者value无法转换成为Short,则返回null
	 * @param key
	 * @return
	 */
	public Short getShort(String key){
		String str = getTool().getProperty(key);
		if(str == null){
			return null;
		}
		Short value = null;
		try {
			value = Short.parseShort(str);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return value;
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			getByte
	 * @description:	加载一个Byte。如果没有对应的key或者value无法转换成为Byte,则返回null
	 * @param key
	 * @return
	 */
	public Byte getByte(String key){
		String str = getTool().getProperty(key);
		if(str == null){
			return null;
		}
		Byte value = null;
		try {
			value = Byte.parseByte(str);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return value;
	}
	
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			getDouble
	 * @description:	加载一个Double。如果没有对应的key或者value无法转换成为Double,则返回null
	 * @param key
	 * @return
	 */
	public Double getDouble(String key){
		String str = getTool().getProperty(key);
		if(str == null){
			return null;
		}
		Double value = null;
		try {
			value = Double.parseDouble(str);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return value;
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			getFloat
	 * @description:	加载一个Float。如果没有对应的key或者value无法转换成为Float,则返回null
	 * @param key
	 * @return
	 */
	public Float getFloat(String key){
		String str = getTool().getProperty(key);
		if(str == null){
			return null;
		}
		Float value = null;
		try {
			value = Float.parseFloat(str);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return value;
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			containsKey
	 * @description:	文件的键值对中是否包含该键
	 * @param key
	 * @return
	 */
	public boolean containsKey(String key){
		return getTool().containsKey(key);
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			containsValue
	 * @description:	文件的键值对中是否包含该值
	 * @param value
	 * @return
	 */
	public boolean containsValue(String value){
		return getTool().containsValue(value);
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			getKeySet
	 * @description:	获取所有的键
	 * @return
	 */
	public Set<Object> getKeySet(){
		return getTool().keySet();
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			getEntrySet
	 * @description:	获取键值对的集合
	 * @return
	 */
	public Set<Entry<Object,Object>> getEntrySet(){
		return getTool().entrySet();
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			setValue
	 * @description:	为key设置值value
	 * @param key
	 * @param value
	 */
	public void setValue(String key,String value){
		getTool().setProperty(key, value);
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			writeToFile
	 * @description:	将已经加载的和改变的所有的东西写入文件中,如果该文件存在则覆盖,没有则创建,使用文件系统默认编码
	 * @param filename	要写入的文件的路径
	 * @return
	 */
	public boolean writeToFile(String filename){
		return writeToFile(filename,System.getProperty("file.encoding"));
	}
	
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			writeToFile
	 * @description:	将已经加载的和改变的所有的东西写入文件中,如果该文件存在则覆盖,没有则创建
	 * @param filename	要写入的文件的路径
	 * @param encoding	要写入的字符串的编码
	 * @return
	 */
	public boolean writeToFile(String filename,String encoding){
		//检查该文件是否存在,如果没有则创建
		File file = new File(filename);
		if(!file.exists()){
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		}
		//创建输出流
		try {
			FileOutputStream stream = new FileOutputStream(file);
			return writeToStream(stream,encoding);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			writeToStream
	 * @description:	将已经加载的和改变的所有的东西写入字节流中,使用文件指定的编码
	 * @param stream	要写入的字节流
	 * @param encoding	字符编码
	 * @return
	 */
	public boolean writeToStream(OutputStream stream,String encoding){
		try {
			OutputStreamWriter writer = new OutputStreamWriter(stream,encoding);
			return writeToWriter(writer);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			writeToStream
	 * @description:	将已经加载的和改变的所有的东西写入字节流中,使用文件系统默认编码
	 * @param stream	要写入的字节流
	 * @return
	 */
	public boolean writeToStream(OutputStream stream){
		return writeToStream(stream,System.getProperty("file.encoding"));
	}
	
	/** 
	 * @author: 			forestqqqq@163.com
	 * @method:			writeToWriter
	 * @description:	将已经加载的和改变的所有东西写入字符流中
	 * @param writer	要写入的字符流
	 * @return
	 */
	public boolean writeToWriter(Writer writer){
		try {
			getTool().store(writer, null);
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
}

 

下面是一个用于测试的属性文件内容,文件名叫test.properties,位于src/test5/目录下:

username=forest
password=forestqqqq
age=10
county=china
city=天津
man=true
hasChild=0
salary=100000000.0

 下面是一个用于测试的类

package test5;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

public class PropertiesTest {
	public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
		PropertiesTool pt = new PropertiesTool();
		String filename = new File("src/test5/test.properties").getAbsolutePath();
		System.out.println("filename==="+filename);
		System.out.println("加载结果==="+pt.loadFile(filename,"UTF-8"));
		System.out.println("**********下面输出一些键值对********");
		System.out.println("username=="+pt.getString("username"));
		System.out.println("city=="+pt.getString("city"));
		System.out.println("age=="+pt.getInteger("age"));
		System.out.println("man=="+pt.getBoolean("man"));
		System.out.println("hasChild=="+pt.getBoolean("hasChild"));
		System.out.println("salary=="+pt.getLong("salary"));
		System.out.println("salary=="+pt.getDouble("salary"));
		System.out.println("salary=="+pt.getFloat("salary"));
		System.out.println("**********下面输出所有的Key********");
		for(Object obj:pt.getKeySet()){
			System.out.println("key="+obj);
		}
		//pt.writeToFile("src/test5/test2.properties");
//		pt.writeToStream(
//				new FileOutputStream("src/test5/test2.properties"),"GBK");
		pt.setValue("username", "forestqqqq");
		pt.setValue("salary", "1000");
		pt.writeToWriter(
				new OutputStreamWriter(
						new FileOutputStream("src/test5/test2.properties"),"GBK"));
	}
}

 运行结果如下:

1),控制台输出:

filename===D:\JavaTest2\Test\src\test5\test.properties
加载结果===true
**********下面输出一些键值对********
username==forest
city==天津
age==10
man==true
hasChild==false
java.lang.NumberFormatException: For input string: "100000000.0"
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Long.parseLong(Unknown Source)
	at java.lang.Long.parseLong(Unknown Source)
	at test5.PropertiesTool.getLong(PropertiesTool.java:202)
	at test5.PropertiesTest.main(PropertiesTest.java:21)
salary==null
salary==1.0E8
salary==1.0E8
**********下面输出所有的Key********
key=age
key=password
key=salary
key=hasChild
key=city
key=county
key=man
key=username

 输出的结果中有异常打印信息,因为salary的值无法转换成为Long类型数据

2)在src/test5/目录下生成文件test2.properties

 

1 楼 blackstreet 22 小时前  
jdk自己就有 java.utilResourceBundle 直接搞定