java properties 类_Java中Properties 类的详细使用

我把你的头像,设置成我的名字,此刻你便与我同在。

我把你的名字,写进我的代码里面,以后,我的世界便存在着你。

一.Properties 类

Properties 类位于 java.util.Properties ,是Java 语言的配置文件所使用的类, Xxx.properties 为Java 语言常见的配置文件,如数据库的配置 jdbc.properties, 系统参数配置 system.properties。 这里,讲解一下Properties 类的具体使用。

以key=value 的 键值对的形式进行存储值。 key值不能重复。

c8be1e87e1212d24c91edf33c352cdf2.png

继承了Hashtable 类,以Map 的形式进行放置值, put(key,value) get(key)

主要方法:

609c272c3f9ddffe303f15af51006fc7.png

这里只讲解一些常用的形式。

二. 打印 JVM 参数

JVM 中可以获取Properties, 来打印输出 JVM 所了解的属性值。

用list() 方法,打印到控制台。

@Test

public void printTest(){

Properties properties=System.getProperties();

properties.list(System.out);

}

常见的有:

9fa9254f53b66f1fd91a6f8196f441ca.png

三.打印自定义.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:\\workspace\\JavaLearn\\src\\jdbc.properties"));

properties.load(input);

properties.list(System.out);

}catch(Exception e){

e.printStackTrace();

}

}

url 被截取了。

379b04143d1f40a322217c40e2dcca70.png

三.二 propertyNames 输出 getClass() 加载

@Test

public void name2Test(){

try{

Properties properties=new Properties(); // 用/文件名, / 表示根目录

InputStream input=PropertiesTest.class.getClass().getResourceAsStream("/jdbc.properties");

properties.load(input);

Enumeration names=(Enumeration) 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();

}

}

90dbe460bbdbfd6c076218c7e1eb9913.png

三.三 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 names=properties.stringPropertyNames();

Iterator 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();

}

}

3099a073626031f304a2a112232bef7e.png

四. 获取值 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();

}

}

输出时,getProperty() 有当前的key值,则输出Key值对应的value 值。

如果没有key值,则输出 null 值。

后面可以跟 default 值,如果没有该值,则输出设置的默认值。

7317138ef17d2b2794021c3d9586955b.png

五. 写入到Properties 文件

五.一 普通写入,中文时乱码

@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();

}

}

9cab01d1487f8135c1e8cbd3c876d069.png

五.二 解决乱码写入的问题

在构建输入流和输出流时,指定编码格式, 编码的格式相同。 如均是 utf-8的形式。

@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();

}

}

测试运行之后:

ab571759573588a6b95b337e690cea4d.png

这样便解决了乱码的问题。

六 . 加载和导出到 xml 配置文件

六.一 导出到 .xml 配置文件 storeToXML

将Properties 类中定义的属性,导出成 .xml 的形式.

@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();

}

}

测试结果为:

902c8b515a4d48983889d023b77565c3.png

用 节点 key为属性, 后面跟值来进行输入。

可按照这种形式,继续添加。

六.二 导出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();

}

}

5810e3c522cc80d4aef97a9526f12752.png

这就是Properties 类的常见用法 。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值