`

通用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
分享到:
评论

相关推荐

    Java工具类导出成jar包

    下面将详细介绍如何通过简单的步骤将Java工具类打包成jar文件。 首先,确保你已经在Java集成开发环境(如Eclipse、IntelliJ IDEA等)中编写并编译了你的Java工具类。这些类应该被组织在一个或者多个包(package)中...

    工具类的jar包集合

    在标签中,"Java开发"指明了这些jar包是用于Java编程语言的,"工具类"是指提供通用功能的类库,"代码"则意味着这个集合包含了实现特定功能的源代码。 现在我们来看一下压缩包内的文件名称列表: 1. `mail.jar`:这...

    jar文件反编译工具

    Java Archive (JAR) 文件是Java平台上的一个特殊文件格式,用于封装多个类文件、资源文件和其他相关数据。当我们遇到不熟悉的JAR文件,想要学习其中的代码逻辑或查找特定功能实现时,就需要使用到“jar文件反编译...

    工具类的jar包

    自己写的一些工具类的打包 主要是文件操作 文件夹路径获取 xml文件操作 json对象转换

    jarjar-方便Java打包工具,自定义修改jar包包名

    其中,"jarjar"是一个非常实用的工具,专为Java程序员设计,用于方便地对JAR文件进行操作,如重命名类、移动类或者合并多个JAR文件。在本文中,我们将详细探讨jarjar工具的使用方法、功能以及其在实际项目中的应用。...

    南大通用数据库驱动 jar 包

    jar文件是一种特殊的文件格式,用于打包Java类库,包括类文件、资源文件等,方便分发和运行。南大通用数据库驱动的jar包内包含了所有必要的类和方法,使得Java程序能够识别并通信于南大通用数据库系统。 南大通用...

    Java modbus工具类jlibmodbus-1.2.9.7.jar

    稀缺工具类jar包。主要用于modbus通讯主从站使用。相较于modbus4j,该jar包再网上比较难找到,需要的可以下载使用!既可以配置主站,也可以配置从站,支持TCP和RTU.该工具包适用于eclipse软件,直接再lib中导入即可...

    jarjar-1.4.jar

    jarjar.jar是一款强大的Java工具,它的主要作用是对jar文件进行操作,包括但不限于重命名类、移动类甚至合并多个jar包。这在多库集成、构建大型项目或维护旧代码库时特别有用。jarjar-1.4.jar是该工具的一个版本,...

    jar文件生成exe文件

    为了解决这个问题,我们可以将JAR文件转换为更通用的可执行文件格式,例如Windows平台下的EXE文件。本文将详细介绍如何将JAR文件生成为EXE文件。 首先,我们需要理解JAR文件和EXE文件的区别。JAR(Java Archive)...

    apk文件中添加jar文件

    Java Archive(JAR)文件是Java平台中用于存储类文件、资源文件和其他相关文件的容器。开发者经常使用JAR文件来打包和分发库,以便在多个项目中复用。 添加JAR文件到APK的步骤如下: 1. **准备JAR文件**:确保你...

    java通用jar包

    Java通用jar包是一种在Java开发中广泛使用的可重用代码库,它包含了预编译的类和资源,便于开发者在自己的项目中导入和使用。jar(Java Archive)是Java平台的标准打包格式,允许将多个Java类文件和其他资源文件集合...

    img和jar包,工具类.rar

    总的来说,“img和jar包,工具类.rar”这个压缩包可能包含了一个用于数据操作的Java项目,其中的图像文件可能是用于界面展示,而.jar文件则封装了项目的类库,工具类则提供了数据处理的便利函数。为了进一步了解这个...

    ab文件解析工具abe

    1. **兼容性**:确保你的`abe.jar`版本与创建`.ab`文件的Unity版本兼容,否则可能会出现解析错误。 2. **安全**:因为`.ab`文件可能包含敏感数据,如游戏资产或用户信息,所以在解包和处理这些文件时,务必保持谨慎...

    雪花算法工具类(java文件下载直接用)

    分布式全局唯一ID生成算法,

    jarjar-1.4.jar.rar

    jarjar工具便是一个这样的利器,它允许开发者将Java类进行重命名、移动或者打包到新的JAR文件中,以实现更高效、更简洁的依赖管理。本文将详细介绍jarjar-1.4版本的使用环境、功能及操作方法。 一、jarjar-1.4的...

    java解析kml文件工具(JavaAPIforKml-2.2.1.jar)

    JavaAPIforKml-2.2.1.jar 解析kml文件的工具包

    打Jar包小工具,运行cmd.bat自动打jar包

    在Java开发过程中,打包应用程序为JAR(Java Archive)文件是一项常见的任务,它将源代码编译后的类文件和其他资源组合成一个独立的可执行文件。"打Jar包小工具"是一个便捷的解决方案,旨在简化这个过程。这个工具...

    JAR包查看器

    1. **浏览内容**:JAR包查看器允许用户直观地浏览JAR文件中的所有文件和目录结构,包括.class文件和其他资源文件,这对于理解和调试程序的结构非常有帮助。 2. **搜索功能**:通过内置的搜索功能,可以快速定位到...

    jar文件转java查看工具

    在IT行业中,jar文件是一种广泛使用的Java字节码格式,它包含了编译后的Java类、资源文件以及元数据。这些文件通常用于打包和分发Java应用程序或库。然而,有时开发者需要深入到jar文件内部,查看其中的源代码,以...

    错误Jar文件扫描工具

    当你用Mevan进行开发的时候,执行的时候报出一个Jar文件的异常,后面跟着一个长度,这就证明你的jar...你可以利用这个工具类对你的仓库进行扫描,并跳过源码jar包,最后会把无效的jar文件的完整目录及文件名打印出来。

Global site tag (gtag.js) - Google Analytics