配置文件类 Properties
来源:互联网 发布:php源码 虚拟股票 编辑:程序博客网 时间:2024/06/07 12:27
配置文件类 Properties
主要用于生产配置文件与读取配置文件信息
public class Properties extends Hashtable<Object,Object>
属性列表中每个键及其对应值都是一个字符串。
setProperty方法:public Object setProperty(String key,String value)调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。参数:key - 要置于属性列表中的键。value - 对应于 key 的值。load方法:public void load(Reader reader)按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。store方法:public void store(Writer writer,String comments)将此 Properties 表中的属性列表(键和元素对)写入输出字符流。参数:writer - 输出字符流 writer。comments - 属性列表的描述。public void store(OutputStream out,String comments)将此 Properties 表中的属性列表(键和元素对)写入输出字节流。getProperty(String key) 用指定的键在此属性列表中搜索属性。
注意:
如果配置文件的信息中有中文,则在使用store方法生成配置文件时只能使用字符流,若使用字节流,默认使用的是iso8859-1码表进行编码存储,会乱码
如果Properties中的内容发生了变化,一定要使用Properties重新生成配置文件
package test;import java.util.*;import java.util.Map.Entry;import java.io.*;public class Demo5 { public static void main(String[] args) throws IOException{ creatProperties(); readProperties(); } //读取配置文件信息 public static void readProperties() throws IOException{ //创建properties对象 Properties properties=new Properties(); //加载配置文件信息到Properties中 properties.load(new FileReader("F:\\test\\persons.properties")); Set<Entry<Object,Object>> entrys=properties.entrySet(); for(Entry<Object,Object> entry:entrys){ System.out.println("键: "+entry.getKey()+" 值:"+entry.getValue()); } //修改"dandan"的值 properties.setProperty("dandan", "000"); //修改之后重新生成配置文件 properties.store(new FileWriter("F:\\test\\persons.properties"), "hello"); } //保存配置文件的信息 public static void creatProperties() throws IOException{ //创建Properties Properties properties=new Properties(); properties.setProperty("gouwa","123"); properties.setProperty("dandan","234"); properties.setProperty("铁蛋","345"); //遍历// Set<Entry<Object,Object>> entrys=properties.entrySet();// for(Entry<Object,Object> entry:entrys){// System.out.println("键:"+entry.getKey()+" 值:"+entry.getValue());// } //使用Properties生成配置文件// properties.store(new FileOutputStream("F:\\person.properties"), "haha");//public void store(OutputStream out, String comments) 参数:out - 输出流 ,comments - 属性列表的描述。 //配置文件中有中文时,要用字符流 properties.store(new FileWriter("F:\\test\\persons.properties"), "hello"); }}
package test;import java.io.*;import java.util.*;/* * 需求:实现Properties实现本软件只能运行3次,超过3次之后提示请购买正版,退出jvm * */public class ProTest { public static void main(String[] args) throws IOException{ File file=new File("F:\\test\\count.properties"); if(!file.exists()){ //配置文件不存在,则创建配置文件 file.createNewFile(); } //创建Properties对象 Properties properties=new Properties(); //把配置文件加载到properties中 properties.load(new FileInputStream(file)); FileOutputStream fileOutputStream=new FileOutputStream(file,true); //运行次数 int count=0; //读取配置文件的运行次数 String value=properties.getProperty("count"); if(value!=null){ count=Integer.parseInt(value); } //判断使用的次数是否已经达到了三次 if(count==3){ System.out.println("你已经超出了试用次数,请购买正版软件.."); System.exit(0); } count++; System.out.println("你已经使用本软件"+count+"次"); properties.setProperty("count",count+""); //使用Properties生成一个配置文件 properties.store(fileOutputStream, "runtime"); }}
0 0
- Properties类读写.properties配置文件
- Properties(配置文件类)
- Properties类与配置文件
- Properties(配置文件类)
- Properties(配置文件类)
- 配置文件类 Properties
- Properties类与配置文件
- Properties(配置文件类)
- Log4J 配置文件 指定类 properties
- 读写properties配置文件的类
- Java配置文件程序->Properties类
- 读取.properties配置文件工具类!!!
- 读取Properties配置文件工具类
- 读取配置文件properties工具类
- 加载properties配置文件工具类
- properties配置文件读取工具类
- 读取properties配置文件工具类
- 配置文件Properties读取类 ResourceBundle
- 【转帖】OpenGL的版本历史和发展 (2013-07-24 14:57:28)
- 299. Bulls and Cows#2(Done)
- Ubuntu16.04 LTS 版安装VMwareTools的步骤和没法挂载目录问题的解决
- 201612-1 试题名称:最大波动(100分)ccf认证
- ListView的下拉刷新和上拉加载
- 配置文件类 Properties
- 在Kali Linux下实战Nmap的秘诀
- java - 继承 ,重写
- 水表项目 水表数字识别率分析
- Nao机器人学习(二)
- 从C到C++(二)
- 机器学习算法学习计划
- 总结
- Neofetch带发行版 Logo 图像的系统信息显示工具发布啦!