欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java中Properties的使用详解

时间:2021-03-07 10:29:56|栏目:JAVA代码|点击:

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<Object,Object>

测试的项目结构如下:

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

name=huhx

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

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

三、Properties的list方法的使用

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

list方法的具体代码:

public void list(PrintStream out) {
out.println("-- listing properties --");
Hashtable h = new Hashtable();
enumerate(h);
for (Enumeration e = h.keys() ; e.hasMoreElements() ;) {
String key = (String)e.nextElement();
String val = (String)h.get(key);
if (val.length() > 40) {
val = val.substring(0, 37) + "...";
}
out.println(key + "=" + val);
}
}

四、Properties的store方法的使用

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

五、Properties的storeToXML方法的使用

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

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

huhx.txt:

#comments
#Thu May 19 19:19:36 CST 2016
name=huhx 

huhx.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>comments</comment>
<entry key="name">huhx</entry>
</properties> 

友情链接,PropertiesTest.java:

package com.huhx.linux;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Properties;
public class PropertiesTest {
public static void main(String[] args) throws Exception {
// 一般Properties的使用
Properties properties = new Properties();
FileInputStream fis = new FileInputStream("huhx.properties");
properties.load(fis);
System.out.println(properties.get("name"));
// 以下是测试的部分
PrintStream printStream = System.out;
properties.list(printStream);
OutputStream outputStream = new FileOutputStream("huhx.txt");
properties.store(outputStream, "comments");
OutputStream outputStream2 = new FileOutputStream("huhx.xml");
properties.storeToXML(outputStream2, "comments");
}
}

上一篇:详解Java中IO字节流基本操作(复制文件)并测试性能

栏    目:JAVA代码

下一篇:自定义BufferedReader的实例

本文标题:Java中Properties的使用详解

本文地址:http://www.codeinn.net/misctech/75789.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有