浅谈Java中Properties类的详细使用

网友投稿 279 2023-01-03

浅谈Java中Properties类的详细使用

一、Properties 类

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

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

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

主要方法:

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

二、打印JVM参数

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

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

@Test

public void printTest(){

Properties properties=System.getProperties();

properties.list(System.out);

}

常见的有:

三、打印自定义.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

3.1、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 被截取了。

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

}

}

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

}

}

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

五、写入到Properties文件

5.1、普通写入,中文时乱码

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

}

}

5.2、解决乱码写入的问题

在构建输入流和输出流时,指定编码格式, 编码的格式相同。 如均是 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();

}

}

测试运行之后:

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

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

6.1、导出到.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();

}

}

测试结果为:

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

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

6.2、导出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();

}

}

以上就是浅谈Java中Properties类的详细使用的详细内容,更多关于Java Properties的资料请关注我们其它相关文章!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:JAVA OutputStreamWriter流的实现
下一篇:腾达快递物流查询单号(达腾飞快递单号查询)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~