1 基本概括
2 主要介绍
2.1 Properties类
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
一个属性列表可包含另一个属性列表作为它的“默认值”;如果未能在原有的属性列表中搜索到属性键,则搜索第二个属性列表。
因为 Properties 继承于 Hashtable ,所以可对 Properties 对象应用 put 和 putAll 方法。但不建议使用这两个方法,因为它们允许调用者插入其键或值不是 String 的项。相反,应该使用 setProperty 方法。如果在“不安全”的 Properties 对象(即包含非 String 的键或值)上调用 store 或 save 方法,则该调用将失败。类似地,如果在“不安全”的Properties 对象(即包含非 String 的键)上调用 propertyNames 或 list 方法,则该调用将失败。
2.2 properties配置文件作用
properties是配置文件。
主要的作用是通过修改配置文件可以方便地修改代码中的参数,实现不用改class文件即可灵活变更参数。解释:java运行中java文件会变成class文件,之后无法通过反编译找到原样的代码,这样的话,如果 java 类中某个参数变更,就很难灵活的实现参数修改,这个时候properties 文件就能很灵活的实现配置,减少代码的维护成本和提高开发效率。
3 简单用例
3.1 打印自定义.properties 文件中的值
在src 目录下,放置 jdbc .properties 文件,是数据库的配置文件。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8
jdbc.username=root
jdbc.password=abc123
list 输出到控制台 用绝对路径加载
@Test
public void name1Test(){
try{
Properties properties=new Properties();
//用的是磁盘符的绝对路径
InputStream input=new BufferedInputStream(new FileInputStream("D:\jdbc.properties"));
properties.load(input);
properties.list(System.out);
}catch(Exception e){
e.printStackTrace();
}
}
}
3.2 propertyNames 输出 getClass() 加载
@Test
public void name2Test(){
try{
Properties properties=new Properties(); // 用/文件名, / 表示根目录
InputStream input=PropertiesTest.class.getClass().getResourceAsStream("/jdbc.properties");
properties.load(input);
Enumeration<String> names=(Enumeration<String>) properties.propertyNames();
while(names.hasMoreElements()){
//这是key值
String key=names.nextElement();
String value=properties.getProperty(key);
System.out.println(key+"="+value);
}catch(Exception e){
e.printStackTrace();
}
}
3.3 stringPropertyNames 输出 getClassLoader 加载 (推荐)
@Test
public void name3Test(){
try{
Properties properties=new Properties();
//直接写src 类路径下的文件名
InputStream input=PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(input);
//把key值转换成set 的形式,遍历set
Set<String> names=properties.stringPropertyNames();
Iterator<String> iterator=names.iterator();
while(iterator.hasNext()){
String key=iterator.next();
String value=properties.getProperty(key);
System.out.println(key+"="+value);
}
catch(Exception e){
e.printStackTrace();
}
}
3.4 获取值 getProperties
@Test
public void name3Test(){
try{
Properties properties=new Properties();
InputStream input=PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(input);
//String value=properties.getProperty("jdbc.url");
String value=properties.getProperty("jdbc.url1","没有该key值");
System.out.println("输出值:"+value);
}catch(Exception e){
e.printStackTrace();
}
}
3.5 普通写入,中文时乱码
@Test
public void writeTest(){
try{
Properties properties=new Properties();
InputStream input=PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(input);
//多添加几个值。
properties.setProperty("name","两个蝴蝶飞");
properties.setProperty("sex","男");
//properties.put("name","两个蝴蝶飞"); 可以用继承Hashtable 的put 方法写入值
// properties.put("sex","男");
//将添加的值,连同以前的值一起写入 新的属性文件里面。
OutputStream out=new FileOutputStream("D:\jdbc.properties");
properties.store(out,"填充数据");
}catch(Exception e){
e.printStackTrace();
}
}
3.6 解决乱码写入的问题
@Test
public void write2Test(){
try{
Properties properties=new Properties();
//用绝对路径
InputStream input=new BufferedInputStream(new FileInputStream("D:\workspace\JavaLearn\src\jdbc.properties"));
properties.load(new InputStreamReader(input,"utf-8"));
//多添加几个值。
properties.setProperty("name","两个蝴蝶飞");
properties.setProperty("sex","男");
OutputStream output=new FileOutputStream("D:\jdbc.properties");
OutputStreamWriter out=new OutputStreamWriter(output,"utf-8");
properties.store(out,"填充数据");
}catch(Exception e){
e.printStackTrace();
}
}
3.7 导出到 .xml 配置文件 storeToXML
@Test
public void xmlWriteTest(){
try{
//处理成编码样式。
Properties properties=new Properties();
//多添加几个值。
properties.setProperty("name","两个蝴蝶飞");
properties.setProperty("sex","男");
OutputStream output=new FileOutputStream("D:\jdbc.xml");
//编码设置成utf-8的形式。
properties.storeToXML(output,"填充到xml","utf-8");
}catch(Exception e){
e.printStackTrace();
}
}
3.8 导出XML 配置文件 loadFromXML
@Test
public void xmlReadTest(){
try{
Properties properties=new Properties();
InputStream input=new BufferedInputStream(new FileInputStream("D:\jdbc.xml"));
properties.loadFromXML(input);
properties.list(System.out);
}catch(Exception e){
e.printStackTrace();
}
}
一起讨论学习的可以的点下 关注,会持续更新 ,文章有用的话可以 收藏 , 转发, 有什么补充可以在下面 评论, 谢谢