读取.properties的内容1

时间:2021-04-01 19:34:57

  属性文件方便于项目进行更改,在项目开发中用的也是非常的普遍,在这里就把属性文件的读取通过代码进行一个小结:

package com.oyy.test;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

public class T3 {
public static void main(String[] args) throws IOException {
//无序读取属性文件的key和value
Properties prop = new Properties();
InputStream in = null;
try {
// 读取属性文件a.properties
in = new BufferedInputStream(new FileInputStream("E:\\Workspaces\\MyEclipse 8.5\\threeFormatsOfTheHttpRequest\\src\\system.properties"));
prop.load(in); // /加载属性列表
//输出key为userName所对应的值:getTokenUser1
// System.out.println(prop.get("userName"));
Iterator<String> it = prop.stringPropertyNames().iterator();
while (it.hasNext()) {
//获取属性文件的key
String key = it.next();
//获取属性文件的value
String value = prop.getProperty(key);
//此时可以将属性文件放入对象或者map集合中 TODO

System.out.println(key + ":" + value);
}
// // 保存属性到b.properties文件,如果属性文件b.properties不存在,则会新生成一个属性文件
// FileOutputStream oFile = new FileOutputStream("E:\\dom4j\\b.properties", true);// true表示追加打开
// prop.setProperty("phone", "10086");
// prop.store(oFile, "The New properties file");
// oFile.close();
}
catch (Exception e) {
System.out.println(e);
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
//logger.error("属性类Properties对象加载的输入流关闭异常");
}
}
}
}