通用Propterties工具类,兼容读jar同级目录的文件。

1.前言。 
  properties文件的写方法比较不好用,折中写了个通用类。 
2.例子。 

Java代码   收藏代码
  1. import java.io.BufferedInputStream;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.util.Enumeration;  
  9. import java.util.HashMap;  
  10. import java.util.Map;  
  11. import java.util.Properties;  
  12.   
  13. /** 
  14.  * 读取Properties综合类 
  15.  *  
  16.  * @author lijn 2014-02-25 
  17.  */  
  18. public class PropertiesUtil {  
  19.     /** 
  20.      * 得到某一个类的路径 
  21.      *  
  22.      * @param name 
  23.      * @return 
  24.      */  
  25.     public static String getPath(Class name) {  
  26.         String strResult = null;  
  27.         if (System.getProperty("os.name").toLowerCase().indexOf("window") > -1) {  
  28.             strResult = name.getResource("/").toString().replace("file:/""")  
  29.                     .replace("%20"" ");  
  30.         } else {  
  31.             strResult = name.getResource("/").toString().replace("file:""")  
  32.                     .replace("%20"" ");  
  33.         }  
  34.         return strResult;  
  35.     }  
  36.   
  37.     /** 
  38.      * 读取所有的property 
  39.      *  
  40.      * @param filename 
  41.      *            properties文件路径 
  42.      * @return 所有的property的集合(map形式) 
  43.      */  
  44.     @SuppressWarnings("unchecked")  
  45.     public static Map<String, String> getPropties(String filename) {  
  46.         if (null == filename) {  
  47.             return null;  
  48.         }  
  49.         String filePath = getPath(PropertiesUtil.class) + filename;  
  50.         Properties props = new Properties();  
  51.         InputStream in = null;  
  52.         try {  
  53.             in = new BufferedInputStream(new FileInputStream(filePath));  
  54.             props.load(in);  
  55.             Map<String, String> map = new HashMap<String, String>();  
  56.             Enumeration en = props.propertyNames();  
  57.             while (en.hasMoreElements()) {  
  58.                 String key = (String) en.nextElement();  
  59.                 String Property = props.getProperty(key);  
  60.                 map.put(key, Property);  
  61.             }  
  62.             return map;  
  63.             // 关闭资源  
  64.         } catch (FileNotFoundException e) {  
  65.             e.printStackTrace();  
  66.         } catch (IOException e) {  
  67.             e.printStackTrace();  
  68.         } finally {  
  69.             try {  
  70.                 in.close();  
  71.             } catch (IOException e) {  
  72.                 // TODO Auto-generated catch block  
  73.                 e.printStackTrace();  
  74.             }  
  75.         }  
  76.         return null;  
  77.     }  
  78.   
  79.     /** 
  80.      * 获取某个property的值 
  81.      *  
  82.      * @param filename 
  83.      *            文件名 
  84.      * @param key 
  85.      *            property的key 
  86.      * @return property的value 
  87.      */  
  88.     public static String setProp(String filename, String key, String value) {  
  89.         if (null == filename || null == key) {  
  90.             return null;  
  91.         }  
  92.         String filePath = getPath(PropertiesUtil.class) + filename;  
  93.         Properties props = new Properties();  
  94.         FileOutputStream out = null;  
  95.         InputStream in = null;  
  96.         try {  
  97.             in = new BufferedInputStream(new FileInputStream(filePath));  
  98.             props.load(in);  
  99.             out = new FileOutputStream(new File(filePath));  
  100.             props.setProperty(key, value);  
  101.             props.store(out, "update");  
  102.             // 关闭资源  
  103.         } catch (FileNotFoundException e) {  
  104.             e.printStackTrace();  
  105.         } catch (IOException e) {  
  106.             e.printStackTrace();  
  107.         } finally {  
  108.             try {  
  109.                 out.close();  
  110.                 in.close();  
  111.             } catch (IOException e) {  
  112.                 // TODO Auto-generated catch block  
  113.                 e.printStackTrace();  
  114.             }  
  115.         }  
  116.         return null;  
  117.     }  
  118.   
  119.     /** 
  120.      * 获取某个property的值 
  121.      *  
  122.      * @param filename 
  123.      *            文件名 
  124.      * @param key 
  125.      *            property的key 
  126.      * @return property的value 
  127.      */  
  128.     public static String getProp(String filename, String key) {  
  129.         if (null == filename || null == key) {  
  130.             return null;  
  131.         }  
  132.         String filePath = getPath(PropertiesUtil.class) + filename;  
  133.         Properties props = new Properties();  
  134.         InputStream in = null;  
  135.         try {  
  136.             in = new BufferedInputStream(new FileInputStream(filePath));  
  137.             props.load(in);  
  138.             return props.getProperty(key);  
  139.             // 关闭资源  
  140.         } catch (FileNotFoundException e) {  
  141.             e.printStackTrace();  
  142.         } catch (IOException e) {  
  143.             e.printStackTrace();  
  144.         } finally {  
  145.             try {  
  146.                 in.close();  
  147.             } catch (IOException e) {  
  148.                 // TODO Auto-generated catch block  
  149.                 e.printStackTrace();  
  150.             }  
  151.         }  
  152.         return null;  
  153.     }  
  154.   
  155.     /** 
  156.      * 需要将config.properies文件放到和本类同级目录下面 
  157.      *  
  158.      * @param filename 
  159.      * @param key 
  160.      * @param value 
  161.      * @return 
  162.      */  
  163.     public static String setJarProp(String filename, String key, String value) {  
  164.         if (null == filename || null == key) {  
  165.             return null;  
  166.         }  
  167.         String filePath = getPath(PropertiesUtil.class) + filename;  
  168.         Properties props = new Properties();  
  169.         FileOutputStream out = null;  
  170.         InputStream in = null;  
  171.         try {  
  172.             in = PropertiesUtil.class.getResourceAsStream("config.properties");  
  173.             // System.out.println(PropertiesUtil.class.getClassLoader().getResource("config.properties"));  
  174.             BufferedInputStream b2 = new BufferedInputStream(in);  
  175.             props.load(b2);  
  176.   
  177.             out = new FileOutputStream(filePath);  
  178.             props.setProperty(key, value);  
  179.             props.store(out, "update");  
  180.             // 关闭资源  
  181.         } catch (FileNotFoundException e) {  
  182.             e.printStackTrace();  
  183.         } catch (IOException e) {  
  184.             e.printStackTrace();  
  185.         } finally {  
  186.             try {  
  187.                 out.close();  
  188.                 in.close();  
  189.             } catch (IOException e) {  
  190.                 // TODO Auto-generated catch block  
  191.                 e.printStackTrace();  
  192.             }  
  193.         }  
  194.         return null;  
  195.     }  
  196.   
  197.     /** 
  198.      * 读写jar外的properties文件 
  199.      *  
  200.      * @param filename 
  201.      * @param key 
  202.      * @param value 
  203.      */  
  204.     public static void setExtralProperty(String filename, String key,  
  205.             String value) {  
  206.         if (null == filename || null == key) {  
  207.             return;  
  208.         }  
  209.         String outPathString = System.getProperty("java.class.path");  
  210.         outPathString = outPathString.substring(0, outPathString  
  211.                 .lastIndexOf("\\") + 1);  
  212.   
  213.         String filePath = outPathString + filename;  
  214.         Properties props = new Properties();  
  215.         FileOutputStream out = null;  
  216.         BufferedInputStream b2 = null;  
  217.         try {  
  218.             b2 = new BufferedInputStream(new FileInputStream(filePath));  
  219.             props.load(b2);  
  220.   
  221.             out = new FileOutputStream(new File(filePath));  
  222.             props.setProperty(key, value);  
  223.             props.store(out, "update");  
  224.             // 关闭资源  
  225.         } catch (FileNotFoundException e) {  
  226.             setProp(filename,key,value);  
  227.         } catch (IOException e) {  
  228.             e.printStackTrace();  
  229.         } finally {  
  230.             try {  
  231.                 if(null!=out){  
  232.                  out.close();  
  233.                 }  
  234.                 if(null!=b2){  
  235.                 b2.close();  
  236.                 }  
  237.             } catch (IOException e) {  
  238.                 // TODO Auto-generated catch block  
  239.                 e.printStackTrace();  
  240.             }  
  241.         }  
  242.   
  243.     }  
  244.     /** 
  245.      * 以行的形式写ja内的文件 
  246.      *  
  247.      * @param filename 
  248.      * @param key 
  249.      * @param value 
  250.      */  
  251.     public static void writeLineInerFile(String filename, String content) {  
  252.         if (null == filename || null == content) {  
  253.             return;  
  254.         }  
  255.         String filePath = getPath(PropertiesUtil.class) + filename;  
  256.       
  257.         FileOutputStream out = null;  
  258.         BufferedInputStream b2 = null;  
  259.         byte[] cont=new byte[4];  
  260.         String contentResult="";  
  261.         try {  
  262.             //先读取源文件的内容,再追加内容  
  263.             b2 = new BufferedInputStream(new FileInputStream(filePath));  
  264.             int res=b2.read(cont);  
  265.             while(res!=-1){  
  266.                 contentResult=contentResult+new String(cont);  
  267.                 res=b2.read(cont);  
  268.             }  
  269.             //追加  
  270.             contentResult+=content;  
  271.             out = new FileOutputStream(new File(filePath));  
  272.             out.write(contentResult.getBytes());  
  273.             // 关闭资源  
  274.         } catch (FileNotFoundException e) {  
  275.         } catch (IOException e) {  
  276.             e.printStackTrace();  
  277.         } finally {  
  278.             try {  
  279.                 if(null!=out){  
  280.                  out.close();  
  281.                 }  
  282.                 if(null!=b2){  
  283.                 b2.close();  
  284.                 }  
  285.             } catch (IOException e) {  
  286.                 // TODO Auto-generated catch block  
  287.                 e.printStackTrace();  
  288.             }  
  289.         }  
  290.   
  291.   
  292.     }  
  293.     /** 
  294.      * 以行的形式写jar外的文件 
  295.      *  
  296.      * @param filename 
  297.      * @param key 
  298.      * @param value 
  299.      */  
  300.     public static void writeLineExtralFile(String filename, String content) {  
  301.         if (null == filename || null == content) {  
  302.             return;  
  303.         }  
  304.         String outPathString = System.getProperty("java.class.path");  
  305.         outPathString = outPathString.substring(0, outPathString  
  306.                 .lastIndexOf("\\") + 1);  
  307.   
  308.         String filePath = outPathString + filename;  
  309.         FileOutputStream out = null;  
  310.         BufferedInputStream b2 = null;  
  311.         byte[] cont=new byte[1024];  
  312.         String contentResult="";  
  313.         try {  
  314.             //先读取源文件的内容,再追加内容  
  315.             b2 = new BufferedInputStream(new FileInputStream(filePath));  
  316.             int res=b2.read(cont);  
  317.             while(res!=-1){  
  318.                 contentResult=contentResult+new String(cont);  
  319.                 res=b2.read(cont);  
  320.             }  
  321.             //追加  
  322.             contentResult+=content;  
  323.             out = new FileOutputStream(filePath);  
  324.             out.write(contentResult.getBytes());  
  325.             // 关闭资源  
  326.         } catch (FileNotFoundException e) {  
  327.             writeLineInerFile(filename,content);  
  328.         } catch (IOException e) {  
  329.             e.printStackTrace();  
  330.         } finally {  
  331.             try {  
  332.                 if(null!=out){  
  333.                  out.close();  
  334.                 }  
  335.                 if(null!=b2){  
  336.                 b2.close();  
  337.                 }  
  338.             } catch (IOException e) {  
  339.                 // TODO Auto-generated catch block  
  340.                 e.printStackTrace();  
  341.             }  
  342.         }  
  343.   
  344.     }  
  345.     /** 
  346.      * 读jar外的properties文件的属性 
  347.      *  
  348.      * @param filename 
  349.      * @param key 
  350.      * @param value 
  351.      */  
  352.     public static String getExtralProperty(String filename, String key) {  
  353.         if (null == filename || null == key) {  
  354.             return null;  
  355.         }  
  356.         String outPathString = System.getProperty("java.class.path");  
  357.         outPathString = outPathString.substring(0, outPathString  
  358.                 .lastIndexOf("\\") + 1);  
  359.   
  360.         String filePath = outPathString + filename;  
  361.         Properties props = new Properties();  
  362.         BufferedInputStream b2=null;  
  363.         try {  
  364.              b2 = new BufferedInputStream(new FileInputStream(filePath));  
  365.             props.load(b2);  
  366.             return props.getProperty(key);  
  367.             // 关闭资源  
  368.         } catch (FileNotFoundException e) {  
  369.           return getProp(filename,key);  
  370.         } catch (IOException e) {  
  371.             e.printStackTrace();  
  372.         } finally {  
  373.             try {  
  374.                 if(null!=b2){  
  375.                     b2.close();  
  376.                 }  
  377.             } catch (IOException e) {  
  378.                 // TODO Auto-generated catch block  
  379.                 e.printStackTrace();  
  380.             }  
  381.         }  
  382.         return null;  
  383.   
  384.     }  
  385.   
  386.     public static void main(String[] args) throws IOException {  
  387.         writeLineExtralFile("error.txt","fiowe");  
  388.   
  389.     }  
  390. }  
  391. 转自:http://nannan408.iteye.com/blog/2026937
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值