您的位置:首页 > 运维架构

读取、写入properties文件

2015-06-06 15:49 267 查看
import java.io.BufferedInputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Enumeration;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Properties;

import java.util.Set;

/**

* 读取Properties文件

* @version V.1 Date: 2008-2-15 18:38:40

*/

public class AnalysisProperties {

private static final String CONFIG_FILE = "admin.properties";

private static final String DATABASE_FILE = "databaseSQL.properties";

/**

* 读取获取properties文件

* @return

* @throws IOException

*/

@SuppressWarnings("rawtypes")

public static Map<String, String> getProperties() throws IOException {

String filePath = getPath(AnalysisProperties.class)+CONFIG_FILE;

Properties pps = new Properties();

Map<String , String> map = new HashMap<String, String>();

InputStream in = new BufferedInputStream(new FileInputStream(filePath));

pps.load(in);

in.close();

Enumeration en = pps.propertyNames(); //得到配置文件的名字

while (en.hasMoreElements()) {

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

String value = pps.getProperty(key);

map.put(key, value);

}

return map;

}

/**

* 读取获取SQl data properties文件

* @return

* @throws IOException

*/

@SuppressWarnings("rawtypes")

public static Map<String, String> getDatabaseProperties() throws IOException {

String filePath = getPath(AnalysisProperties.class)+DATABASE_FILE;

Properties pps = new Properties();

Map<String , String> map = new HashMap<String, String>();

InputStream in = new BufferedInputStream(new FileInputStream(filePath));

pps.load(in);

in.close();

Enumeration en = pps.propertyNames(); //得到配置文件的名字

while (en.hasMoreElements()) {

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

String value = pps.getProperty(key);

map.put(key, value);

}

return map;

}

/**

* 写入文件

* @param filePath

* @param key

* @param value

* @throws IOException

*/

public static void WriteProperties(Map<String, String> map) throws IOException{

String filePath = getPath(AnalysisProperties.class)+CONFIG_FILE;

Properties pps = new Properties();

InputStream in = new FileInputStream(filePath);

pps.load(in);

in.close();

OutputStream out = new FileOutputStream(filePath);

Set<Entry<String, String>> key =map.entrySet();

for (Iterator iterator = key.iterator(); iterator.hasNext();) {

Entry<String, String> entry = (Entry<String, String>) iterator.next();

pps.setProperty(entry.getKey(), entry.getValue());

}

pps.store(out, "Update data");

out.close();

// System.out.println("获取修改后的属性值:"+entry.getKey()+"=" + pps.getProperty(entry.getKey()));

}

private static String getPath(Class<? extends AnalysisProperties> name) {

String strResult = null;

if (System.getProperty("os.name").toLowerCase().indexOf("window") > -1) {

strResult = name.getResource("/").toString().replace("file:/", "")

.replace("%20", " ");

} else {

strResult = name.getResource("/").toString().replace("file:", "").replace("%20", " ");

}

return strResult;

}

public static String getConfigFile() {

return CONFIG_FILE;

}

public static String getDatabaseFile() {

return DATABASE_FILE;

}

public static void main(String[] args) throws IOException {

String filePath = getPath(AnalysisProperties.class)+CONFIG_FILE;

Properties pps = new Properties();

InputStream in = new BufferedInputStream(new FileInputStream(filePath));

pps.load(in);

Enumeration en = pps.propertyNames(); //得到配置文件的名字

while (en.hasMoreElements()) {

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

String value = pps.getProperty(key);

System.out.println(key+"="+value);

}

Map<String, String > map = getProperties() ;

System.out.println("admin===="+map.get("account"));

String json= SpliceJson.getCreateJSONData(map);

System.out.println(json);

Map<String, String > dataMap = getDatabaseProperties() ;

Set<Entry<String, String>> key= dataMap.entrySet();

for (Iterator iterator = key.iterator(); iterator.hasNext();) {

Entry<String, String> entry = (Entry<String, String>) iterator.next();

System.out.println( entry.getKey()+"==="+entry.getValue());

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java properties