java遍历读取文件内容_【Java编程】写入、读取、遍历Properties文件

在Java开发中通常我们会存储配置參数信息到属性文件。这种属性文件能够是拥有键值对的属性文件,也能够是XML文件。关于XML文件的操作,请參考博文【Java编程】DOM XML Parser 解析、遍历、创建XML。

在该篇博文中,我将展示怎样向属性文件写入键值对。怎样读取属性文件里的键值对,怎样遍历属性文件。

1、向属性文件里写入键值对

4c269c0345039af2740b077451f36ad9.png

特别注意:

Properties类调用setProperty方法将键值对保存到内存中。此时能够通过getProperty方法读取,propertyNames()方法进行遍历,可是并没有将键值对持久化到属性文件里。故须要调用store()方法持久化键值对到属性文件里。这里的store方法相似于Android SharedPreferences的commit()方法。

package com.andieguo.propertiesdemo;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Date;

import java.util.Enumeration;

import java.util.Properties;

import junit.framework.TestCase;

public class PropertiesTester extends TestCase {

public void writeProperties() {

Properties properties = new Properties();

OutputStream output = null;

try {

output = new FileOutputStream("config.properties");

properties.setProperty("url", "jdbc:mysql://localhost:3306/");

properties.setProperty("username", "root");

properties.setProperty("password", "root");

properties.setProperty("database", "bbs");//保存键值对到内存

properties.store(output, "andieguo modify" + new Date().toString());// 保存键值对到文件里

} catch (IOException io) {

io.printStackTrace();

} finally {

if (output != null) {

try {

output.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

运行单元測试后。属性文件内容例如以下:

6pdDl0DRdw

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYW5kaWVfZ3Vv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />

2、读取属性文件里的键值对

cde6ddae16b6fd44fb1c33562ab61479.png

public class PropertiesTester extends TestCase {

public void loadProperties() {

Properties properties = new Properties();

InputStream input = null;

try {

input = new FileInputStream("config.properties");//载入Java项目根路径下的配置文件

properties.load(input);// 载入属性文件

System.out.println("url:" + properties.getProperty("url"));

System.out.println("username:" + properties.getProperty("username"));

System.out.println("password:" + properties.getProperty("password"));

System.out.println("database:" + properties.getProperty("database"));

} catch (IOException io) {

} finally {

if (input != null) {

try {

input.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

运行单元測试方法。console输出的output例如以下:

6pdDl0DRdw

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYW5kaWVfZ3Vv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />

3、遍历属性文件里的键值对

package com.andieguo.propertiesdemo;

import java.io.IOException;

import java.io.InputStream;

import java.util.Enumeration;

import java.util.Map.Entry;

import java.util.Properties;

import java.util.Set;

import junit.framework.TestCase;

public class PropertiesTester extends TestCase {

public void printAll() {

Properties prop = new Properties();

InputStream input = null;

try {

String filename = "config.properties";

input = getClass().getClassLoader().getResourceAsStream(filename);

if (input == null) {

System.out.println("Sorry, unable to find " + filename);

return;

}

prop.load(input);

//方法一:

Set keys = prop.keySet();//返回属性key的集合

for(Object key:keys){

System.out.println("key:"+key.toString()+",value:"+prop.get(key));

}

//方法二:

Set> entrys =prop.entrySet();//返回的属性键值对实体

for(Entry entry:entrys){

System.out.println("key:"+entry.getKey()+",value:"+entry.getValue());

}

//方法三:

Enumeration

> e = prop.propertyNames();

while (e.hasMoreElements()) {

String key = (String) e.nextElement();

String value = prop.getProperty(key);

System.out.println("Key:" + key + ",Value:" + value);

}

} catch (IOException ex) {

ex.printStackTrace();

} finally {

if (input != null) {

try {

input.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

4、其它方法

public void list(PrintStream out)

将属性列表输出到指定的输出流。此方法对调试非常实用。

public void storeToXML(OutputStream os,Stringcomment) throws IOException

发出一个表示此表中包括的全部属性的 XML 文档。

5、參考

6、你可能感兴趣的文章

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值