1.前言。
properties文件的写方法比较不好用,折中写了个通用类。
2.例子。
- 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.util.Enumeration;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Properties;
- /**
- * 读取Properties综合类
- *
- * @author lijn 2014-02-25
- */
- public class PropertiesUtil {
- /**
- * 得到某一个类的路径
- *
- * @param name
- * @return
- */
- public static String getPath(Class 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;
- }
- /**
- * 读取所有的property
- *
- * @param filename
- * properties文件路径
- * @return 所有的property的集合(map形式)
- */
- @SuppressWarnings("unchecked")
- public static Map<String, String> getPropties(String filename) {
- if (null == filename) {
- return null;
- }
- String filePath = getPath(PropertiesUtil.class) + filename;
- Properties props = new Properties();
- InputStream in = null;
- try {
- in = new BufferedInputStream(new FileInputStream(filePath));
- props.load(in);
- Map<String, String> map = new HashMap<String, String>();
- Enumeration en = props.propertyNames();
- while (en.hasMoreElements()) {
- String key = (String) en.nextElement();
- String Property = props.getProperty(key);
- map.put(key, Property);
- }
- return map;
- // 关闭资源
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- in.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return null;
- }
- /**
- * 获取某个property的值
- *
- * @param filename
- * 文件名
- * @param key
- * property的key
- * @return property的value
- */
- public static String setProp(String filename, String key, String value) {
- if (null == filename || null == key) {
- return null;
- }
- String filePath = getPath(PropertiesUtil.class) + filename;
- Properties props = new Properties();
- FileOutputStream out = null;
- InputStream in = null;
- try {
- in = new BufferedInputStream(new FileInputStream(filePath));
- props.load(in);
- out = new FileOutputStream(new File(filePath));
- props.setProperty(key, value);
- props.store(out, "update");
- // 关闭资源
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- out.close();
- in.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return null;
- }
- /**
- * 获取某个property的值
- *
- * @param filename
- * 文件名
- * @param key
- * property的key
- * @return property的value
- */
- public static String getProp(String filename, String key) {
- if (null == filename || null == key) {
- return null;
- }
- String filePath = getPath(PropertiesUtil.class) + filename;
- Properties props = new Properties();
- InputStream in = null;
- try {
- in = new BufferedInputStream(new FileInputStream(filePath));
- props.load(in);
- return props.getProperty(key);
- // 关闭资源
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- in.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return null;
- }
- /**
- * 需要将config.properies文件放到和本类同级目录下面
- *
- * @param filename
- * @param key
- * @param value
- * @return
- */
- public static String setJarProp(String filename, String key, String value) {
- if (null == filename || null == key) {
- return null;
- }
- String filePath = getPath(PropertiesUtil.class) + filename;
- Properties props = new Properties();
- FileOutputStream out = null;
- InputStream in = null;
- try {
- in = PropertiesUtil.class.getResourceAsStream("config.properties");
- // System.out.println(PropertiesUtil.class.getClassLoader().getResource("config.properties"));
- BufferedInputStream b2 = new BufferedInputStream(in);
- props.load(b2);
- out = new FileOutputStream(filePath);
- props.setProperty(key, value);
- props.store(out, "update");
- // 关闭资源
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- out.close();
- in.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return null;
- }
- /**
- * 读写jar外的properties文件
- *
- * @param filename
- * @param key
- * @param value
- */
- public static void setExtralProperty(String filename, String key,
- String value) {
- if (null == filename || null == key) {
- return;
- }
- String outPathString = System.getProperty("java.class.path");
- outPathString = outPathString.substring(0, outPathString
- .lastIndexOf("\\") + 1);
- String filePath = outPathString + filename;
- Properties props = new Properties();
- FileOutputStream out = null;
- BufferedInputStream b2 = null;
- try {
- b2 = new BufferedInputStream(new FileInputStream(filePath));
- props.load(b2);
- out = new FileOutputStream(new File(filePath));
- props.setProperty(key, value);
- props.store(out, "update");
- // 关闭资源
- } catch (FileNotFoundException e) {
- setProp(filename,key,value);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if(null!=out){
- out.close();
- }
- if(null!=b2){
- b2.close();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- /**
- * 以行的形式写ja内的文件
- *
- * @param filename
- * @param key
- * @param value
- */
- public static void writeLineInerFile(String filename, String content) {
- if (null == filename || null == content) {
- return;
- }
- String filePath = getPath(PropertiesUtil.class) + filename;
- FileOutputStream out = null;
- BufferedInputStream b2 = null;
- byte[] cont=new byte[4];
- String contentResult="";
- try {
- //先读取源文件的内容,再追加内容
- b2 = new BufferedInputStream(new FileInputStream(filePath));
- int res=b2.read(cont);
- while(res!=-1){
- contentResult=contentResult+new String(cont);
- res=b2.read(cont);
- }
- //追加
- contentResult+=content;
- out = new FileOutputStream(new File(filePath));
- out.write(contentResult.getBytes());
- // 关闭资源
- } catch (FileNotFoundException e) {
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if(null!=out){
- out.close();
- }
- if(null!=b2){
- b2.close();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- /**
- * 以行的形式写jar外的文件
- *
- * @param filename
- * @param key
- * @param value
- */
- public static void writeLineExtralFile(String filename, String content) {
- if (null == filename || null == content) {
- return;
- }
- String outPathString = System.getProperty("java.class.path");
- outPathString = outPathString.substring(0, outPathString
- .lastIndexOf("\\") + 1);
- String filePath = outPathString + filename;
- FileOutputStream out = null;
- BufferedInputStream b2 = null;
- byte[] cont=new byte[1024];
- String contentResult="";
- try {
- //先读取源文件的内容,再追加内容
- b2 = new BufferedInputStream(new FileInputStream(filePath));
- int res=b2.read(cont);
- while(res!=-1){
- contentResult=contentResult+new String(cont);
- res=b2.read(cont);
- }
- //追加
- contentResult+=content;
- out = new FileOutputStream(filePath);
- out.write(contentResult.getBytes());
- // 关闭资源
- } catch (FileNotFoundException e) {
- writeLineInerFile(filename,content);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if(null!=out){
- out.close();
- }
- if(null!=b2){
- b2.close();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- /**
- * 读jar外的properties文件的属性
- *
- * @param filename
- * @param key
- * @param value
- */
- public static String getExtralProperty(String filename, String key) {
- if (null == filename || null == key) {
- return null;
- }
- String outPathString = System.getProperty("java.class.path");
- outPathString = outPathString.substring(0, outPathString
- .lastIndexOf("\\") + 1);
- String filePath = outPathString + filename;
- Properties props = new Properties();
- BufferedInputStream b2=null;
- try {
- b2 = new BufferedInputStream(new FileInputStream(filePath));
- props.load(b2);
- return props.getProperty(key);
- // 关闭资源
- } catch (FileNotFoundException e) {
- return getProp(filename,key);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if(null!=b2){
- b2.close();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return null;
- }
- public static void main(String[] args) throws IOException {
- writeLineExtralFile("error.txt","fiowe");
- }
- }
- 转自:http://nannan408.iteye.com/blog/2026937