Properties(配置文件类)
2.如果Properties中的内容发生了变化,一定要重新使用Properties生成配置文件,否则配置文件信息不回发生变化。
package com.cn.properties;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import javax.sound.sampled.AudioFormat.Encoding;
/**
* Author:Liu Zhiyong(QQ:1012421396)
* Version:Version_1
* Date:2016年7月31日20:11:10
* Desc:
Properties(配置文件类):主要用于生产配置文件与读取配置文件的信息。
Properties要注意的细节:
1.若果配置文件的信息一旦使用了中文,那么在使用store方法生成配置文件的时候只能使用字符流解决,如果使用字节流生成配置文件的话,
默认使用的是iso-8859-1码表进行编码存储,这时候会出现乱码。
2.如果Properties中的内容发生了变化,一定要重新使用Properties生成配置文件,否则配置文件信息不回发生变化。
*/
public class Demo1 {
public static void main(String[] args) throws IOException {
createProperties();
readProperties();
}
//保存配置文件的信息
public static void createProperties() throws IOException, IOException{
//创建Properties
Properties properties = new Properties();
properties.setProperty("木丁西", "1234");
properties.setProperty("刘先森", "2343");
properties.setProperty("木sir", "4344");
properties.setProperty("流先生", "6666");
properties.setProperty("Mr.liu", "9999");
Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();
Iterator<Entry<Object, Object>> iterator = entrySet.iterator();
while(iterator.hasNext()){
Entry<Object, Object> next = iterator.next();
System.out.println(next.getKey() + "\t" + next.getValue());
}
//使用Properties生成配置文件
/* FileOutputStream fileOutputStream = new FileOutputStream("src/com/cn/properties/properties.properties");
properties.store(fileOutputStream, "这里存放的是用户名和密码");//第一个参数是输出流对象("."当前路径),第二个参数是使用一个字符串描述这个配置文件的信息。
fileOutputStream.close();
*/
FileWriter fileWriter = new FileWriter("src/com/cn/properties/properties.properties");
properties.store(fileWriter, "这里存放的是用户名和密码");
fileWriter.close();
}
//读取配置文件的信息
public static void readProperties() throws IOException{
//创建Properties
Properties properties = new Properties();
FileInputStream fileInputStream = new FileInputStream("src/com/cn/properties/properties.properties");
properties.load(fileInputStream);
Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();
Iterator<Entry<Object, Object>> iterator = entrySet.iterator();
while(iterator.hasNext()){
Entry<Object, Object> next = iterator.next();
System.out.println(next.getKey() + "\t" + next.getValue());
}
System.out.println("并没有修改成功==================================");
//修改密码 这里实际的文件无法修改成功。
properties.setProperty("木丁西", "改了");
Set<Map.Entry<Object, Object>> entrySet1 = properties.entrySet();
Iterator<Entry<Object, Object>> iterator1 = entrySet1.iterator();
while(iterator1.hasNext()){
Entry<Object, Object> next = iterator1.next();
System.out.println(next.getKey() + "\t" + next.getValue());
}
System.out.println("修改成功==================================");
//把修改后的Properties文件再生成一个配置文件
properties.store(new FileOutputStream("src/com/cn/properties/properties.properties"), "2222222222222");
Set<Map.Entry<Object, Object>> entrySet2 = properties.entrySet();
Iterator<Entry<Object, Object>> iterator2 = entrySet2.iterator();
while(iterator2.hasNext()){
Entry<Object, Object> next = iterator2.next();
System.out.println(next.getKey() + "\t" + next.getValue());
}
}
}
package com.cn.properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
/**
* Author:Liu Zhiyong(QQ:1012421396)
* Version:Version_1
* Date:2016年8月1日12:41:29
* Desc:
需求:使用Properties实现本软件只能运行3次,超过3次后就提示购买正版,退出JVM
*/
public class Demo2 {
public static void main(String[] args) throws IOException {
File file = new File("src/com/cn/properties/record.properties");
if(!file.exists()){
//如果配置文件不存在,则创建该文件
file.createNewFile();
}
// FileOutputStream fileOutputStream = new FileOutputStream(file);//这句话若放在这里就会出错,创建前会先清空文件
//创建Properties对象
Properties properties = new Properties();
//把配置文件的信息加载到properties中
properties.load(new FileInputStream(file));
// FileOutputStream fileOutputStream = new FileOutputStream(file);//这句话若放在这里还是会出错
//定该变量用于保存软件的运行次数
int times = 0;
//读取配置文件中的登陆次数
String value =properties.getProperty("count");
if(value != null){
times = Integer.valueOf(value);
}
if(times >= 3){
System.out.println("您的次数已经用完,请购买正版。。");
System.exit(0);
}
// FileOutputStream fileOutputStream = new FileOutputStream(file);//这句话最早能放这里,在虚拟机退出前。 √
times++;
System.out.println("您已经使用了软件" + times + "次。");
properties.setProperty("count", Integer.toString(times));
//使用Properties生成一个配置文件
// properties.store(fileOutputStream, "the login times");
properties.store(new FileOutputStream(file), "the login times");
}
}
Properties(配置文件类)相关推荐
- Java中的Properties类详解Properties配置文件
1.Properties类是什么? Properties(Java.util.Properties),该类主要用于读取Java的配置文件,不同的编程语言有自己所支持的配置文件,配置文件中很多变量是经常 ...
- 【应用】Properties类与Properties配置文件的读写
1.Properties类与Properties配置文件 什么是Properties类 Properties(Java.util.Properties),该类主要用于读取Java的配置文件,不同的编程 ...
- java修改配置文件参数_在java类中获取在.properties配置文件中设置的参数
如何获取.properties配置文件中的参数,我在网上查了半天没弄明白,后来在以前的项目中找到了,就写下来,避免遗忘. 1.配置文件:message_product.properties total ...
- java properties文件 安全_java 数据库读取工具类(读取config.properties配置文件)[包含线程安全] | 学步园...
java 数据库读取工具类(读取config.properties配置文件)[包含线程安全] 数据库读取工具类 package com.db; import java.sql.Connection; ...
- java基础50 配置文件类(Properties)
1. 配置文件类Properties的概念 主要生产配置文件与读取配置文件的信息 2.Properties要注意的细节 1.如果配置文件一旦使用了中文,那么在使用store方法生产的配置文件额时候字符 ...
- java基础5:工厂模式、单例模式、File文件类、递归、IO流、Properties配置文件、网络编程、利用IO流模拟注册登录功能、关于反射、JDK动态代理
1.工厂模式 23种java设计模式之一 1)提供抽象类(基类) 2)提供一些子类,完成方法重写 3)提供一个接口:完成具体子类的实例化对象的创建,不能直接new子类,构造函数私有化. 优点:具体的子 ...
- java配置文件实现方式_java相关:详解Spring加载Properties配置文件的四种方式
java相关:详解Spring加载Properties配置文件的四种方式 发布于 2020-4-29| 复制链接 摘记: 一.通过 context:property-placeholder 标签实现配 ...
- Java读取Properties配置文件
目录 1.Properties类与Properties配置文件 2.Properties中的主要方法 3.示例 1.Properties类与Properties配置文件 Properties类继承自H ...
- Java 读写Properties配置文件
转自:https://www.cnblogs.com/xudong-bupt/p/3758136.html 1.Properties类与Properties配置文件 Properties类继承自Has ...
最新文章
- (已解决)ImportError attempted relative import with no known parent package
- C++创建 可以实例化但不能继承的类
- ic 卡获取帐号apdu指令_非接触IC卡片APDU指令系统介绍..docx
- python开发的著名软件公司_软件开发公司_软件外包_项目外包平台基于Python开发一个全文检索系统...
- 理解CapsuleNetwork2
- 对一个存储过程语法的解读
- 电信、联通合建 5G,将会碰出怎样的火花?
- 单片机学习都时候需要注意的步骤-依葫芦画瓢
- 最好用的 3 个 Windows EPUB 阅读器推荐
- 【其他】vue项目集成富文本编辑器
- 格雷希尔快速密封接头在燃油泵密封性能检测的作用
- U盘格式化后容量变小恢复方法
- 【Mysql 第11章_数据处理之增删改】
- 阿里的防DDoS能力有多强,小蚁带你了解一下
- 写完的文档有多少个字?字数统计在word哪里
- 批量下载sra文件linux,Linux下从NCBI批量下载SRA数据的sra和aspera方法
- 分布式数据库HBase
- WEB安全之:Access 数据库 SQL 注入
- 腾讯云网站备案咨询类解答:网站是否需要备案?
- 牛客网 剑指Offer,一些值得记住的小题(五)
热门文章
- 宝塔 Nginx免费防火墙 post 参数值长度超过20w已被系统拦截(post_max_size)
- EEGNET:开源的可视化分析脑网络工具
- iPhone手机获取uuid
- RNN的神奇之处(The Unreasonable Effectiveness of Recurrent Neural Networks)
- 2015-2020全国各市空气质量与天气情况匹配
- 利用spyder软件,采用python处理excel的教程
- VS2013 Windows7(X64被测试)安装方法 离线安装
- 一文搞懂CDN加速原理
- 政务服务热线中的大数据应用 ---- 实现政务热线大数据价值的路线图
- 金仓数据库KingbaseES 迁移工具—PL/SQL中Oracle和KingbaseES 的对比