Properties 类存在于包 Java.util 中,该类继承自 Hashtable ,它提供了几个主要的方法:
1. load (nputStream inStream) ,从输入流中读取属性列表(键和元素对)。
2. getProperty(String key), 用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value 。
3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put。他通过调用基类的put方法来设置 键 - 值对。 通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。
4. store(OutputStream out,String comments) ,以适合使用 load 方法加载到 Properties表中的格式,将此 Properties 表中的属性列 表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5. clear() ,清除所有装载的 键 - 值对。该方法在基类中提供。 有了以上几个方法我们就可以对 .properties 文件进行操作了!
java的properties文件需要放到classpath下面,这样程序才能读取到,有关classpath实际上就是java类或者库的存放路径: 在java工程中,properties放到class文件一块; 在web应用中,最简单的方法是放到web应用的WEB-INFclasses 目录下即可,也可以放在其他文件夹下面,这时候需要在设置classpath环境变量的时候,将这个文件夹路径加到classpath变量中,这样也也可以读取到 。
在此,你需要对classpath有个深刻理解,classpath绝非系统中刻意设定的那个系统环境变量,WEB-INFclasses其实也是,java工程的 class文件目录也是。
001 |
import java.io.BufferedInputStream; |
002 |
import java.io.FileInputStream; |
003 |
import java.io.FileOutputStream; |
004 |
import java.io.IOException; |
005 |
import java.io.InputStream; |
006 |
import java.io.OutputStream; |
007 |
import java.util.Enumeration; |
008 |
import java.util.Properties; |
009 |
010 |
public class PropertiesTools { |
011 |
/** |
012 |
* 读取properties的全部信息 |
013 |
*/ |
014 |
public static void readProperties(String fileNamePath) throws IOException { |
015 |
Properties props = new Properties(); |
016 |
InputStream in = null ; |
017 |
try { |
018 |
in = new BufferedInputStream( new FileInputStream(fileNamePath)); |
019 |
// 如果将in改为下面的方法,必须要将.Properties文件和此class类文件放在同一个包中 |
020 |
// in = propertiesTools.class.getResourceAsStream(fileNamePath); |
021 |
props.load(in); |
022 |
Enumeration en = props.propertyNames(); |
023 |
while (en.hasMoreElements()) { |
024 |
String key = (String) en.nextElement(); |
025 |
String Property = props.getProperty(key); |
026 |
System.out.println(key + "=" + Property); |
027 |
} |
028 |
} catch (Exception e) { |
029 |
if ( null != in) |
030 |
in.close(); |
031 |
e.printStackTrace(); |
032 |
} finally { |
033 |
if ( null != in) |
034 |
in.close(); |
035 |
} |
036 |
} |
037 |
038 |
/** |
039 |
* 根据key读取value |
040 |
*/ |
041 |
public static String getValue(String fileNamePath, String key) |
042 |
throws IOException { |
043 |
Properties props = new Properties(); |
044 |
InputStream in = null ; |
045 |
try { |
046 |
in = new FileInputStream(fileNamePath); |
047 |
// 如果将in改为下面的方法,必须要将.Properties文件和此class类文件放在同一个包中 |
048 |
// in = propertiesTools.class.getResourceAsStream(fileNamePath); |
049 |
props.load(in); |
050 |
String value = props.getProperty(key); |
051 |
// 有乱码时要进行重新编码 |
052 |
// new String(props.getProperty("name").getBytes("ISO-8859-1"), |
053 |
// "GBK"); |
054 |
return value; |
055 |
} catch (IOException e) { |
056 |
e.printStackTrace(); |
057 |
return null ; |
058 |
} finally { |
059 |
if ( null != in) |
060 |
in.close(); |
061 |
} |
062 |
063 |
} |
064 |
065 |
/** |
066 |
* 写入properties信息 |
067 |
*/ |
068 |
public static void setProperties(String fileNamePath, String parameterName, |
069 |
String parameterValue) throws IOException { |
070 |
Properties prop = new Properties(); |
071 |
InputStream in = null ; |
072 |
OutputStream out = null ; |
073 |
try { |
074 |
in = new FileInputStream(fileNamePath); |
075 |
// 如果将in改为下面的方法,必须要将.Properties文件和此class类文件放在同一个包中 |
076 |
// in = propertiesTools.class.getResourceAsStream(fileNamePath); |
077 |
prop.load(in); |
078 |
out = new FileOutputStream(fileNamePath); |
079 |
prop.setProperty(parameterName, parameterValue); |
080 |
prop.store(out, parameterName); // 保存 |
081 |
out.flush(); |
082 |
out.close(); |
083 |
} catch (IOException e) { |
084 |
e.printStackTrace(); |
085 |
} finally { |
086 |
if ( null != in) |
087 |
in.close(); |
088 |
if ( null != out) |
089 |
out.close(); |
090 |
} |
091 |
} |
092 |
093 |
public static void main(String[] args) throws IOException { |
094 |
// 方法一: |
095 |
// String fileNamePath = "info.properties"; |
096 |
// 上面不变,用类中注释掉的in = |
097 |
// TestProperties.class.getResourceAsStream(fileNamePath);方法,必须要将.Properties文件和此 |
098 |
099 |
// class类文件放在同一个包中 |
100 |
101 |
// 方法二: |
102 |
String fileNamePath = "config//info.properties" ; |
103 |
// 这个方法需要在项目的要目录下建立config文件夹,然后把properties文件放在config下 |
104 |
105 |
// 取值 |
106 |
System.out.println( "取username的值:" + getValue(fileNamePath, "username" )); |
107 |
System.out.println( "取age的值:" + getValue(fileNamePath, "age" )); |
108 |
// 写值 |
109 |
setProperties(fileNamePath, "age" , "21" ); |
110 |
// 取此文件所有配置信息 |
111 |
readProperties(fileNamePath); |
112 |
} |
113 |
114 |
} |