今天闲来无事就复习了下java基础,看了一下属性文件的读写,整理了一下代码,包括属性文件的读写,贴上来,以备查验。
package com.wxh.study.properties.util;
import java.io.BufferedInputStream;
import java.io.File;
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.Properties;
/**
* 属性文件读写类
*
* @Company 深圳市****有限公司
* @author ***
* @version 2014-06-19
* @since V0.1
*/
public class PropertiesUtil {
/**创建一个Properties对象用于存放读取到的属性文件内容*/
//private static Properties props = new Properties();
/**
* 根据key获取属性文件中的value值<br/>
* @param filePath 文件路径<br/>
* @author ***
* @version 2014-06-19
* @since V0.1
*/
public static Properties getProperties(String filePath){
Properties props = new Properties();
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(filePath));
props.load(in);
return props;
} catch (FileNotFoundException e) {
throw new RuntimeException("找不到路径为: "+filePath+"的属性文件");
} catch (IOException e) {
return null;
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 根据key获取属性文件中的value值<br/>
* @param key key值<br/>
* @param props 属性对象<br/>
* @author ***
* @version 2014-06-19
* @since V0.1
*/
public static String getValue(String key,Properties props){
return props.getProperty(key);
}
/**
* 根据key获取属性文件中的value值<br/>
* @param filePath 文件路径<br/>
* @param key key值<br/>
* @author ***
* @version 2014-06-19
* @since V0.1
*/
public static String getPropertiesVlaueByKey(String filePath,String key){
Properties props = new Properties();
InputStream in = null;
//src路径
//ClassLoader loader = PropertiesUtil.class.getClassLoader();
try {
in = new BufferedInputStream(new FileInputStream(filePath));
//如果属性文件在src根目录下,则使用此方法获取文件
//in = loader.getResourceAsStream("system.properties");
props.load(in);
return props.getProperty(key);
} catch (FileNotFoundException e) {
throw new RuntimeException("找不到路径为: "+filePath+"的属性文件");
} catch (IOException e) {
return "";
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 根据文件路径、key、value值写入属性文件<br/>
* @param key key值<br/>
* @param value value值
* @param filePath 文件 路径<br/>
* @author ***
* @version 2014-06-19
* @since V0.1
*/
public static boolean writePropertiesFile(String filePath, String key,String value) {
Properties props = new Properties();
try {
// 如果文件不存在,创建一个新的
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
InputStream fis = new FileInputStream(filePath);
// 从输入流中读取属性列表(键和元素对)
props.load(fis);
fis.close();
OutputStream fos = new FileOutputStream(filePath);
props.setProperty(key, value);
// 以适合使用 load 方法加载到 Properties 表中的格式,
// 将此 Properties 表中的属性列表(键和元素对)写入输出流
props.store(fos, key);
fos.close(); // 关闭流
return true;
} catch (IOException e) {
System.err.println("Visit " + filePath + " for updating "
+ key + " value error");
return false;
}
}
public static void main(String[] args) {
//String value = PropertiesUtil.getPropertiesVlaueByKey("F:\\workspace\\CALENDARCENTER\\src\\system.properties", "ftp_ip");
//System.out.println(value);
PropertiesUtil.writePropertiesFile("F:\\workspace\\CALENDARCENTER\\src\\system.properties", "wxh_test", "success...");
}
}