Java中Properties的使用详解

简介:


这篇文章主要介绍了Java中Properties的使用详解的相关资料,需要的朋友可以参考下。

Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支 持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。今天,我们就开始Properties的使用。

Java中Properties的使用

Properties的文档说明:

The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.

Properties类的描述:

public class Properties extends Hashtable

测试的项目结构如下:

一、在huhx.properties文件中,我们为也方便,加入一条数据:

 
  1. name=huhx  

二、将huhx.properties文件加载读取,得到相应的属性

 
  1. Properties properties = new Properties(); 
  2. FileInputStream fis = new FileInputStream("huhx.properties"); 
  3. properties.load(fis); 
  4. System.out.println(properties.get("name"));  

三、Properties的list方法的使用

 
  1. PrintStream printStream = System.out; 
  2. properties.list(printStream);  

list方法的具体代码:

 
  1. public void list(PrintStream out) { 
  2. out.println("-- listing properties --"); 
  3. Hashtable h = new Hashtable(); 
  4. enumerate(h); 
  5. for (Enumeration e = h.keys() ; e.hasMoreElements() ;) { 
  6. String key = (String)e.nextElement(); 
  7. String val = (String)h.get(key); 
  8. if (val.length() > 40) { 
  9. valval = val.substring(0, 37) + "..."; 
  10. out.println(key + "=" + val); 

四、Properties的store方法的使用

 
  1. OutputStream outputStream = new FileOutputStream("huhx.txt"); 
  2. properties.store(outputStream, "comments");  

五、Properties的storeToXML方法的使用

 
  1. OutputStream outputStream2 = new FileOutputStream("huhx.xml"); 
  2. properties.storeToXML(outputStream2, "comments");  
 

六、最终生成的文件如下:

 
  1. huhx.txt: 
  2. #comments 
  3. #Thu May 19 19:19:36 CST 2016 
  4. name=huhx  
  5. huhx.xml: 
  6. <?xml version="1.0" encoding="UTF-8" standalone="no"?> 
  7. <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> 
  8. <properties> 
  9. <comment>comments</comment> 
  10. <entry key="name">huhx</entry> 
  11. </properties>  

 友情链接,PropertiesTest.java:

 
  1. package com.huhx.linux; 
  2. import java.io.FileInputStream; 
  3. import java.io.FileOutputStream; 
  4. import java.io.OutputStream; 
  5. import java.io.PrintStream; 
  6. import java.util.Properties; 
  7. public class PropertiesTest { 
  8. public static void main(String[] args) throws Exception { 
 

// 一般Properties的使用

 
  1. Properties properties = new Properties(); 
  2. FileInputStream fis = new FileInputStream("huhx.properties"); 
  3. properties.load(fis); 
  4. System.out.println(properties.get("name")); 
 

// 以下是测试的部分

 
  1. PrintStream printStream = System.out; 
  2. properties.list(printStream); 
  3. OutputStream outputStream = new FileOutputStream("huhx.txt"); 
  4. properties.store(outputStream, "comments"); 
  5. OutputStream outputStream2 = new FileOutputStream("huhx.xml"); 
  6. properties.storeToXML(outputStream2, "comments"); 

以上所述是小编给大家介绍的Java中Properties的使用详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!


作者:huhx

来源:51CTO

相关文章
|
6月前
|
XML JavaScript 前端开发
【Java】Spring Boot中的配置properties 和 yml 的区别
【Java】Spring Boot中的配置properties 和 yml 的区别
|
1月前
|
SpringCloudAlibaba Java Maven
【问题篇】Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/
【问题篇】Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/
13 2
|
8月前
|
Java
Java IO流之Properties类的详解
Java IO流之Properties类的详解
57 0
|
4月前
|
XML Java 数据格式
Java中的Properties类详解Properties配置文件
Java中的Properties类详解Properties配置文件
40 0
|
7月前
|
Java
Java集合Properties
Java集合Properties
50 0
|
8月前
|
Java
Java 中Map接口及其实现子类HashMap,Hashtable,Properties,TreeMap类的详解(二)
Java 中Map接口及其实现子类HashMap,Hashtable,Properties,TreeMap类的详解
25 0
|
8月前
|
Java
Java 中Map接口及其实现子类HashMap,Hashtable,Properties,TreeMap类的详解(一)
Java 中Map接口及其实现子类HashMap,Hashtable,Properties,TreeMap类的详解
39 0
|
8月前
|
XML 存储 安全
Java源码类 - Properties类及多种读取方式
Java源码类 - Properties类及多种读取方式
145 0
|
8月前
java202304java学习笔记第六十六天-ssm-mybatis核心配置文件-properties标签
java202304java学习笔记第六十六天-ssm-mybatis核心配置文件-properties标签
21 0
|
10月前
|
Java Spring
java202304java学习笔记第六十天-ssm-spring配置文件-spring加载properties文件2
java202304java学习笔记第六十天-ssm-spring配置文件-spring加载properties文件2
46 0