对properties文件的操作

来源:互联网 发布:linux脚本命令大全 编辑:程序博客网 时间:2024/05/02 06:47
在做程序时,往往会有一些固定的或长期的值,这些值或许在将来才会被更改。

    由于程序完成时,把这些值写在代码,将来更改起不方便。(而且容易忘记在代码中什么地方,哪个文件中时)所以,我们建议将这些值写入配置文件中。

    今天就让我们一起来看看,在 JAVA 中对 properties 这种配置文件的操作吧。

1、properties 文件

#ParseZUrlNum.properties
#Tue Nov 15 11:52:15 CST 2005
nextnum=360
firstnum=73
secondnum=72

<script type="text/javascript"><!-- google_ad_client = "pub-6429139736808079"; google_ad_width = 300; google_ad_height = 250; google_ad_format = "300x250_as"; google_ad_type = "text_image"; google_ad_channel =""; google_color_border = "B4D0DC"; google_color_bg = "ECF8FF"; google_color_link = "0000CC"; google_color_url = "008000"; google_color_text = "6F6F6F"; //--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>

2、读写文件

{

ParseNZ pnz = new ParseNZ();

Properties properties = new Properties();

try {

            File f = new File("ParseZUrlNum.properties");
            properties = pnz.getProperties ("e:/work/product/src/com/yesky/productattribute/p/z/model/ParseZUrlNum.properties");
} catch (Exception e)
{
            e.printStackTrace();
}
String firstnum = properties.getProperty("firstnum");
String secondnum = properties.getProperty("secondnum");
String nextnum = properties.getProperty("nextnum");

int first=Integer.parseInt(firstnum);
int second=Integer.parseInt(secondnum);
int next=Integer.parseInt(nextnum);

System.out.println("firstnum=" + firstnum);
System.out.println("secondnum=" + secondnum);
System.out.println("nextnum=" + nextnum);

pnz.setProperties(first,second,next);

}

3、ParseNZ 类

public class ParseNZ{
    Properties p = new Properties();

    public Properties getProperties(String filename) throws IOException {

        ClassLoader cl = this.getClass().getClassLoader();
        FileInputStream input;
        // if(cl!=null)
        //input=cl.getResourceAsStream(filename);
        input = new FileInputStream(filename);
        //else
        //input=ClassLoader.getSystemResourceAsStream(filename);

        p.load(input);
        return p;

    }

   

    public void setProperties(int first, int second, int next) {
        p.setProperty("firstnum",String.valueOf(first));
        p.setProperty("secondnum",String.valueOf(second));
        p.setProperty("nextnum",String.valueOf(next));

        File file = new File ("e:/work/product/src/com/yesky/productattribute/p/z/model/ParseZUrlNum.properties");
        try {
              OutputStream fos = new FileOutputStream(file);
              p.store(fos, "ParseZUrlNum.properties");
              fos.close();
        } catch (FileNotFoundException ex) {
            System.out.println("file is NULL !!!");
              ex.printStackTrace();
        } catch (IOException ex) {
            System.out.println("IO is Error !!!");
              ex.printStackTrace();
        }
    }

}

本例中地址是写死的。可改为如: File file = new File(System.getProperty("user.dir")
                                 + "/EMweb/WEB-INF/admininfo.properties");

这里面的关键是System.getProperty("user.dir") 返回当前工作目录。

 

一个小例子

/**
 * AWT Sample application
 *
 * @author 
 * @version 1.00 07/01/06
 */
 import java.util.Properties;
 import java.io.*;
public class PropertyTest 
{
    
    public static void main(String[] args) 
    {
     //属性对象settings存储了一系列的键和值信息
        Properties settings=new Properties();
        //键和值信息来源于某一文件,故要关联此文件
        try{
          settings.load(new FileInputStream("count.txt"));
         }catch(Exception e)
         {
          settings.setProperty("count",String.valueOf(0));
         }
        //然后打印出信息 
        int c=Integer.parseInt(settings.getProperty("count"))+1;//count 是count.txt中的关键字
        System.out.println("这是第"+c+"次运行");
        
        //然后将次数保存起来,以备下次用
        //保存在了属性Properties对象settings中
        settings.setProperty("count",new Integer(c).toString());
        //为了数据的持久性,然后将其保存在文件中
        try{
         settings.store(new FileOutputStream("count.txt"),"Application has been used:");
        }catch(IOException e)
        {
         e.printStackTrace();
        }
        
    }
}
说明:其实,市面上的一些付费软件,用的有免费试用期。当用户的试用期到期后,会提示你要付费,否则不能再继续使用。可以实现记录用户的使用次数的功能类似于Properties。只不过记录使用次数的文件信息被开放人员隐藏了,比如写到了注册表中等。只要你找到后,将其删除,那恭喜你,还可以继续使用哈。

 
原创粉丝点击