posts - 33,  comments - 17,  trackbacks - 0
 1/*
 2操作属性文件,可以为我们的程序带来更方便的移植性,下面是一个示例,可以读、写、更改属性
 3读采用了两种方式,一种是采用Properties类,另外一种是采用资源绑定类ResourceBundle类,
 4下面是源程序,里面有详细的注释:
 5*/

 6import java.io.FileInputStream;
 7import java.io.FileOutputStream;
 8import java.io.InputStream;
 9import java.util.Properties;
10import java.util.ResourceBundle;
11/**
12 *对属性文件(xx.properties)的操作
13 *注:属性文件一定要放在当前工程的根目录下,也就是放在与src目录在同一个目录下(我的JDevelop
14 *是这样的)
15 */

16public class OperatePropertiesFile {
17    public OperatePropertiesFile() {
18    }

19    /**
20     *采用Properties类取得属性文件对应值
21     *@parampropertiesFileNameproperties文件名,如a.properties
22     *@parampropertyName属性名
23     *@return根据属性名得到的属性值,如没有返回""
24     */

25    public static String getValueByPropertyName(String propertiesFileName,String propertyName) {
26        String s="";
27        Properties p=new Properties();//加载属性文件读取类
28        FileInputStream in;
29        try {
30            //propertiesFileName如test.properties
31            in = new FileInputStream(propertiesFileName);//以流的形式读入属性文件
32            p.load(in);//属性文件将该流加入的可被读取的属性中
33            in.close();//读完了关闭
34            s=p.getProperty(propertyName);//取得对应的属性值
35        }
 catch (Exception e) {
36            e.printStackTrace();
37        }

38        return s;
39    }

40    /**
41     *采用ResourceBundel类取得属性文件对应值,这个只能够读取,不可以更改及写新的属性
42     *@parampropertiesFileNameWithoutPostfixproperties文件名,不带后缀
43     *@parampropertyName属性名
44     *@return根据属性名得到的属性值,如没有返回""
45     */

46    public static String getValueByPropertyName_(String propertiesFileNameWithoutPostfix,String propertyName) {
47        String s="";
48        //如属性文件是test.properties,那此时propertiesFileNameWithoutPostfix的值就是test
49        ResourceBundle bundel = ResourceBundle.getBundle(propertiesFileNameWithoutPostfix);
50        s=bundel.getString(propertyName);
51        return s;
52    }

53    /**
54     *更改属性文件的值,如果对应的属性不存在,则自动增加该属性
55     *@parampropertiesFileNameproperties文件名,如a.properties
56     *@parampropertyName属性名
57     *@parampropertyValue将属性名更改成该属性值
58     *@return是否操作成功
59     */

60    public static boolean changeValueByPropertyName(String propertiesFileName,String propertyName,String propertyValue) {
61        boolean writeOK=true;
62        Properties p=new Properties();
63        InputStream in;
64        try {
65            
66            in = new FileInputStream(propertiesFileName);
67            p.load(in);//
68            in.close();
69            p.setProperty(propertyName,propertyValue);//设置属性值,如不属性不存在新建
70            //p.setProperty("testProperty","testPropertyValue");
71            FileOutputStream out=new FileOutputStream(propertiesFileName);//输出流
72            p.store(out,"");//设置属性头,如不想设置,请把后面一个用""替换掉
73            out.flush();//清空缓存,写入磁盘
74            out.close();//关闭输出流
75        }
 catch (Exception e) {
76            e.printStackTrace();
77        }

78        return writeOK;
79    }

80}

81
posted on 2008-07-23 17:54 scea2009 阅读(439) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 

<2008年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

常用链接

留言簿(1)

随笔分类

随笔档案

PL/SQL存储过程与函数

搜索

  •  

最新评论

阅读排行榜

评论排行榜