首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2013-07-08 
加载和写入properties属性文件的工具类import java.io.Fileimport java.io.FileInputStreamimport java.

加载和写入properties属性文件的工具类
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=forestpassword=forestqqqqage=10county=chinacity=天津man=truehasChild=0salary=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==forestcity==天津age==10man==truehasChild==falsejava.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==nullsalary==1.0E8salary==1.0E8**********下面输出所有的Key********key=agekey=passwordkey=salarykey=hasChildkey=citykey=countykey=mankey=username

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

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

?

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

热点排行