Properties(配置文件类)

来源:互联网 发布:斧子演示软件下载 编辑:程序博客网 时间:2024/05/16 14:18
Properties(配置文件类):主要用于生产配置文件与读取配置文件的信息。
Properties要注意的细节:
    1.若果配置文件的信息一旦使用了中文,那么在使用store方法生成配置文件的时候只能使用字符流解决,如果使用字节流生成配置文件的话,
        默认使用的是iso-8859-1码表进行编码存储,这时候会出现乱码。
    2.如果Properties中的内容发生了变化,一定要重新使用Properties生成配置文件,否则配置文件信息不回发生变化。
  1. package com.cn.properties;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.Iterator;
  8. import java.util.Map;
  9. import java.util.Map.Entry;
  10. import java.util.Properties;
  11. import java.util.Set;
  12. import javax.sound.sampled.AudioFormat.Encoding;
  13. /**
  14. * Author:Liu Zhiyong(QQ:1012421396)
  15. * Version:Version_1
  16. * Date:2016年7月31日20:11:10
  17. * Desc:
  18. Properties(配置文件类):主要用于生产配置文件与读取配置文件的信息。
  19. Properties要注意的细节:
  20. 1.若果配置文件的信息一旦使用了中文,那么在使用store方法生成配置文件的时候只能使用字符流解决,如果使用字节流生成配置文件的话,
  21. 默认使用的是iso-8859-1码表进行编码存储,这时候会出现乱码。
  22. 2.如果Properties中的内容发生了变化,一定要重新使用Properties生成配置文件,否则配置文件信息不回发生变化。
  23. */
  24. public class Demo1 {
  25. public static void main(String[] args) throws IOException {
  26. createProperties();
  27. readProperties();
  28. }
  29. //保存配置文件的信息
  30. public static void createProperties() throws IOException, IOException{
  31. //创建Properties
  32. Properties properties = new Properties();
  33. properties.setProperty("木丁西", "1234");
  34. properties.setProperty("刘先森", "2343");
  35. properties.setProperty("木sir", "4344");
  36. properties.setProperty("流先生", "6666");
  37. properties.setProperty("Mr.liu", "9999");
  38. Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();
  39. Iterator<Entry<Object, Object>> iterator = entrySet.iterator();
  40. while(iterator.hasNext()){
  41. Entry<Object, Object> next = iterator.next();
  42. System.out.println(next.getKey() + "\t" + next.getValue());
  43. }
  44. //使用Properties生成配置文件
  45. /*FileOutputStream fileOutputStream = new FileOutputStream("src/com/cn/properties/properties.properties");
  46. properties.store(fileOutputStream, "这里存放的是用户名和密码");//第一个参数是输出流对象("."当前路径),第二个参数是使用一个字符串描述这个配置文件的信息。
  47. fileOutputStream.close();
  48. */
  49. FileWriter fileWriter = new FileWriter("src/com/cn/properties/properties.properties");
  50. properties.store(fileWriter, "这里存放的是用户名和密码");
  51. fileWriter.close();
  52. }
  53. //读取配置文件的信息
  54. public static void readProperties() throws IOException{
  55. //创建Properties
  56. Properties properties = new Properties();
  57. FileInputStream fileInputStream = new FileInputStream("src/com/cn/properties/properties.properties");
  58. properties.load(fileInputStream);
  59. Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();
  60. Iterator<Entry<Object, Object>> iterator = entrySet.iterator();
  61. while(iterator.hasNext()){
  62. Entry<Object, Object> next = iterator.next();
  63. System.out.println(next.getKey() + "\t" + next.getValue());
  64. }
  65. System.out.println("并没有修改成功==================================");
  66. //修改密码 这里实际的文件无法修改成功。
  67. properties.setProperty("木丁西", "改了");
  68. Set<Map.Entry<Object, Object>> entrySet1 = properties.entrySet();
  69. Iterator<Entry<Object, Object>> iterator1 = entrySet1.iterator();
  70. while(iterator1.hasNext()){
  71. Entry<Object, Object> next = iterator1.next();
  72. System.out.println(next.getKey() + "\t" + next.getValue());
  73. }
  74. System.out.println("修改成功==================================");
  75. //把修改后的Properties文件再生成一个配置文件
  76. properties.store(new FileOutputStream("src/com/cn/properties/properties.properties"), "2222222222222");
  77. Set<Map.Entry<Object, Object>> entrySet2 = properties.entrySet();
  78. Iterator<Entry<Object, Object>> iterator2 = entrySet2.iterator();
  79. while(iterator2.hasNext()){
  80. Entry<Object, Object> next = iterator2.next();
  81. System.out.println(next.getKey() + "\t" + next.getValue());
  82. }
  83. }
  84. }
  1. package com.cn.properties;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.util.Date;
  8. import java.util.Iterator;
  9. import java.util.Map.Entry;
  10. import java.util.Properties;
  11. import java.util.Set;
  12. /**
  13. * Author:Liu Zhiyong(QQ:1012421396)
  14. * Version:Version_1
  15. * Date:2016年8月1日12:41:29
  16. * Desc:
  17. 需求:使用Properties实现本软件只能运行3次,超过3次后就提示购买正版,退出JVM
  18. */
  19. public class Demo2 {
  20. public static void main(String[] args) throws IOException {
  21. File file = new File("src/com/cn/properties/record.properties");
  22. if(!file.exists()){
  23. //如果配置文件不存在,则创建该文件
  24. file.createNewFile();
  25. }
  26. //FileOutputStream fileOutputStream = new FileOutputStream(file);//这句话若放在这里就会出错,创建前会先清空文件
  27. //创建Properties对象
  28. Properties properties = new Properties();
  29. //把配置文件的信息加载到properties中
  30. properties.load(new FileInputStream(file));
  31. //FileOutputStream fileOutputStream = new FileOutputStream(file);//这句话若放在这里还是会出错
  32. //定该变量用于保存软件的运行次数
  33. int times = 0;
  34. //读取配置文件中的登陆次数
  35. String value =properties.getProperty("count");
  36. if(value != null){
  37. times = Integer.valueOf(value);
  38. }
  39. if(times >= 3){
  40. System.out.println("您的次数已经用完,请购买正版。。");
  41. System.exit(0);
  42. }
  43. //FileOutputStream fileOutputStream = new FileOutputStream(file);//这句话最早能放这里,在虚拟机退出前。 √
  44. times++;
  45. System.out.println("您已经使用了软件" + times + "次。");
  46. properties.setProperty("count", Integer.toString(times));
  47. //使用Properties生成一个配置文件
  48. //properties.store(fileOutputStream, "the login times");
  49. properties.store(new FileOutputStream(file), "the login times");
  50. }
  51. }
0 0
原创粉丝点击