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

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

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

一.Properties 类

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

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

2bf42d492d7edda98d29d3301dbf477c.png

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

主要方法:

879b60c4b5ed830d8063ebbb0620611d.png

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

二. 打印 JVM 参数

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

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

@Test

public void printTest(){

Properties properties=System.getProperties();

properties.list(System.out);

}

常见的有:

f320813dbce69a5a91ddfd6d9f714649.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 被截取了。

f77179e531a6ffe18ab5dd603d1ec383.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();

}

}

36c4442b9174339ed9c4ca27db24ba6c.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();

}

}

c6961dc0a1ee1346475008c1674b0b36.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 值,如果没有该值,则输出设置的默认值。

5aaf76a9a3b4bfaeaac49367a55bd813.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();

}

}

a2529419cc3929f4e48f41d1dcf98f9e.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();

}

}

测试运行之后:

ea857c694619336470ffd0b9141bb0f1.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();

}

}

测试结果为:

0b54417b4256aaab9944118cf0bdae53.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();

}

}

f6f2d2e71543a0ec8eec1bd7ca932e29.png

这就是Properties 类的常见用法 。

到此这篇关于Java中Properties 类的详细使用的文章就介绍到这了,更多相关Properties 类使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值