java基础50 配置文件类(Properties)
2024-10-15 16:34:54
1、 配置文件类Properties的概念
主要生产配置文件与读取配置文件的信息
2、Properties要注意的细节
1.如果配置文件一旦使用了中文,那么在使用store方法生产的配置文件额时候字符流解决,如果使用字节流生产的配置文件的话,默认使用的编码是iso8895-1码表经行编码存储,这个时候会出现乱码.
2.如果Properties中内容发生了改变,一定要重新使用Properties生成配置文件,否则配置文件不会发生改变.
3、实例
package com.dhb.file; import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set; /**
* @author DSHORE / 2018-7-18
*
*/
public class Demo24 {
public static void main(String[] args) throws Exception {
//createProperties();
readProperties();
}
//读取配置文件的信息
public static void readProperties() throws Exception, IOException{
//创建Properties对象
Properties properties=new Properties();
//加载配置文件信息到Properties里面
properties.load(new FileReader("F:\\person.properties"));
//遍历
/*Set<Entry<Object,Object>> entrys=properties.entrySet();
for (Entry<Object, Object> entry : entrys) {
System.out.println("键:"+entry.getKey()+",值:"+entry.getValue());
//修改密码
//把修改后的properties在生成一个配置文件
properties.setProperty("李四","001");
properties.store(new FileWriter("F:\\person.properties"), "This is QQ account and password properties");
}*/
//修改密码
//把修改后的properties在生成一个配置文件
properties.setProperty("李四","");
properties.store(new FileWriter("F:\\person.properties"), "This is QQ account and password properties");
}
//创建配置文件
public static void createProperties() throws FileNotFoundException, IOException{
//创建一个配置文件
Properties properties=new Properties();
properties.setProperty("张三", "");
properties.setProperty("李四", "");
properties.setProperty("王五","");
//遍历Properties
/*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"), "hehe");//第一个参数是一个输出流对象,第二参数是描述这个配置文件的信息
properties.store(new FileWriter("F:\\person.properties"), "This is QQ account and password properties");
}
}
运行结果图
原创作者:DSHORE 作者主页:http://www.cnblogs.com/dshore123/ 原文出自:https://www.cnblogs.com/dshore123/p/9328488.html 欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!) |
最新文章
- C# 打印PDF文件之使用不同打印机打印所有页面或部分页面
- 修改AndroidStudio中的Logcat中的默认设置
- 输入整数n(n<;=10000),表示接下来将会输入n个实数,将这n个实数存入数组a中。请定义一个数组拷贝函数将数组a中的n个数拷贝到数组b中。
- noj [1475] Bachelor (找1的个数)
- React学习笔记(三) 组件传值
- 使用Calendar获取近三年的财务信息
- struts2 OGNL 表达式
- 【动态规划】【KMP】HDU 5763 Another Meaning
- oracle单行函数之通用函数
- UNIX网络编程——进程间通信概述
- CSS3 box-sizing的作用
- 关于crontab
- C++基础算法学习——N皇后问题
- 控件_AnalogClock
- 利用cookies跳过登陆验证码
- Windows下的Python 3.6.1的下载与安装(适合32bits和64bits)(图文详解)
- spark快速大数据分析
- bsgs算法详解
- MySQL在linux上(cmake)的source code安装方法
- laravel 拾遗 中间件
热门文章
- Go interface{}、类型断言
- js中apply(thisArg, [argsArray])的参数与ArrayLike的关系
- 解题:HEOI 2013 SAO
- 理解jquery的$.extend()、$.fn和$.fn.extend()的区别及用法
- java基础-数组的折半查找原理
- MySQL搭建环境
- layoutSubviews何时被调用
- 科学计算三维可视化---Traits介绍
- PHP里echo print print_r的区别
- 浅谈splay的双旋