对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
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。只不过记录使用次数的文件信息被开放人员隐藏了,比如写到了注册表中等。只要你找到后,将其删除,那恭喜你,还可以继续使用哈。
- 对properties文件的操作
- java对properties文件的操作
- java对properties文件的操作
- java 对properties 文件的写操作
- Java对properties文件的读写操作
- Android 对 properties文件的读写操作
- Java 对 Properties 文件的操作
- java对properties文件的读写操作
- Java对Properties文件的操作
- Java中如何对properties文件的操作
- 对Properties文件的增删改查操作工具类
- 对ApplicationResources.properties的操作
- java对properties的操作
- Java对.properties文件(即java.util.Properties类)的一些操作
- Java对Properties文件操作小结
- 对Properties文件进行读写操作
- Properties 文件的简单操作
- Java对properties配置文件的操作
- Internet相关开发(VC)
- 终于登录了CSDN开通了我的博客
- 2007.03.23
- 如果利用C#获取系统有关环境和属性
- C#学习笔记(1)--了解.NET
- 对properties文件的操作
- Delphi2007 for Win32 值得期待
- Community Server专题八:MemberRole之Membership
- 无知者无畏——中国程序员和印度程序员的对话
- MDA与传统软件开发周期对比之我见
- 客户端JScript+XSLT+XML Data Islands排序
- c#学习笔记(一)拆箱 装箱
- eclipse中設置Lomboz
- Community Server专题八:MemberRole之Membership深入篇