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. }

Properties(配置文件类)相关推荐

  1. Java中的Properties类详解Properties配置文件

    1.Properties类是什么? Properties(Java.util.Properties),该类主要用于读取Java的配置文件,不同的编程语言有自己所支持的配置文件,配置文件中很多变量是经常 ...

  2. 【应用】Properties类与Properties配置文件的读写

    1.Properties类与Properties配置文件 什么是Properties类 Properties(Java.util.Properties),该类主要用于读取Java的配置文件,不同的编程 ...

  3. java修改配置文件参数_在java类中获取在.properties配置文件中设置的参数

    如何获取.properties配置文件中的参数,我在网上查了半天没弄明白,后来在以前的项目中找到了,就写下来,避免遗忘. 1.配置文件:message_product.properties total ...

  4. java properties文件 安全_java 数据库读取工具类(读取config.properties配置文件)[包含线程安全] | 学步园...

    java 数据库读取工具类(读取config.properties配置文件)[包含线程安全] 数据库读取工具类 package com.db; import java.sql.Connection; ...

  5. java基础50 配置文件类(Properties)

    1. 配置文件类Properties的概念 主要生产配置文件与读取配置文件的信息 2.Properties要注意的细节 1.如果配置文件一旦使用了中文,那么在使用store方法生产的配置文件额时候字符 ...

  6. java基础5:工厂模式、单例模式、File文件类、递归、IO流、Properties配置文件、网络编程、利用IO流模拟注册登录功能、关于反射、JDK动态代理

    1.工厂模式 23种java设计模式之一 1)提供抽象类(基类) 2)提供一些子类,完成方法重写 3)提供一个接口:完成具体子类的实例化对象的创建,不能直接new子类,构造函数私有化. 优点:具体的子 ...

  7. java配置文件实现方式_java相关:详解Spring加载Properties配置文件的四种方式

    java相关:详解Spring加载Properties配置文件的四种方式 发布于 2020-4-29| 复制链接 摘记: 一.通过 context:property-placeholder 标签实现配 ...

  8. Java读取Properties配置文件

    目录 1.Properties类与Properties配置文件 2.Properties中的主要方法 3.示例 1.Properties类与Properties配置文件 Properties类继承自H ...

  9. Java 读写Properties配置文件

    转自:https://www.cnblogs.com/xudong-bupt/p/3758136.html 1.Properties类与Properties配置文件 Properties类继承自Has ...

最新文章

  1. (已解决)ImportError attempted relative import with no known parent package
  2. C++创建 可以实例化但不能继承的类
  3. ic 卡获取帐号apdu指令_非接触IC卡片APDU指令系统介绍..docx
  4. python开发的著名软件公司_软件开发公司_软件外包_项目外包平台基于Python开发一个全文检索系统...
  5. 理解CapsuleNetwork2
  6. 对一个存储过程语法的解读
  7. 电信、联通合建 5G,将会碰出怎样的火花?
  8. 单片机学习都时候需要注意的步骤-依葫芦画瓢
  9. 最好用的 3 个 Windows EPUB 阅读器推荐
  10. 【其他】vue项目集成富文本编辑器
  11. 格雷希尔快速密封接头在燃油泵密封性能检测的作用
  12. U盘格式化后容量变小恢复方法
  13. 【Mysql 第11章_数据处理之增删改】
  14. 阿里的防DDoS能力有多强,小蚁带你了解一下
  15. 写完的文档有多少个字?字数统计在word哪里
  16. 批量下载sra文件linux,Linux下从NCBI批量下载SRA数据的sra和aspera方法
  17. 分布式数据库HBase
  18. WEB安全之:Access 数据库 SQL 注入
  19. 腾讯云网站备案咨询类解答:网站是否需要备案?
  20. 牛客网 剑指Offer,一些值得记住的小题(五)

热门文章

  1. 宝塔 Nginx免费防火墙 post 参数值长度超过20w已被系统拦截(post_max_size)
  2. EEGNET:开源的可视化分析脑网络工具
  3. iPhone手机获取uuid
  4. RNN的神奇之处(The Unreasonable Effectiveness of Recurrent Neural Networks)
  5. 2015-2020全国各市空气质量与天气情况匹配
  6. 利用spyder软件,采用python处理excel的教程
  7. VS2013 Windows7(X64被测试)安装方法 离线安装
  8. 一文搞懂CDN加速原理
  9. 政务服务热线中的大数据应用 ---- 实现政务热线大数据价值的路线图
  10. 金仓数据库KingbaseES 迁移工具—PL/SQL中Oracle和KingbaseES 的对比