一、Java Properties类
Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。像Python支持的配置文件是.ini文件,同样,它也有自己读取配置文件的类ConfigParse,方便程序员或用户通过该类的方法来修改.ini配置文件。在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用"#"来注释。
Properties类继承自Hashtable,如下:
它提供了几个主要的方法:
1. getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
2. load ( InputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。
3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
4. store ( OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。
二、Java读取Properties文件
Java读取Properties文件的方法有很多,详见: Java读取Properties文件的六种方法
但是最常用的还是通过java.lang.Class类的getResourceAsStream(String name)方法来实现,如下可以这样调用:
InputStream in = getClass().getResourceAsStream("资源Name");作为我们写程序的,用此一种足够。
或者下面这种也常用:
InputStream in = new BufferedInputStream(new FileInputStream(filepath));
---------------------------------------------------------分割线-----------------------------------------------
一、读取配置文件的六种方法
1。使用java.util.Properties类的load()方法
示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);
2。使用java.util.ResourceBundle类的getBundle()方法
示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
3。使用java.util.PropertyResourceBundle类的构造函数
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);
4。使用class变量的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例: InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);
补充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
二、对应代码
-
package com.test.modul.utils;
-
-
import java.io.BufferedInputStream;
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.FileNotFoundException;
-
import java.io.IOException;
-
import java.io.InputStream;
-
import java.util.Locale;
-
import java.util.Properties;
-
import java.util.PropertyResourceBundle;
-
import java.util.ResourceBundle;
-
-
/**
-
* 加载properties文件的方式
-
*
-
* @author wjzuo
-
*
-
*/
-
public class LoadPropertiesFileUtil {
-
-
private static String basePath = "src/main/java/com/test/modul/utils/prop.properties";
-
private static String path = "";
-
-
/**
-
* 一、 使用java.util.Properties类的load(InputStream in)方法加载properties文件
-
*
-
* @return
-
*/
-
public static String getPath1() {
-
-
try {
-
InputStream in = new BufferedInputStream( new FileInputStream(
-
new File(basePath)));
-
Properties prop = new Properties();
-
-
prop.load(in);
-
-
path = prop.getProperty( "path");
-
-
} catch (FileNotFoundException e) {
-
System.out.println( "properties文件路径书写有误,请检查!");
-
e.printStackTrace();
-
} catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
-
return path;
-
}
-
-
/**
-
* 二、 使用java.util.ResourceBundle类的getBundle()方法
-
* 注意:这个getBundle()方法的参数只能写成包路径+properties文件名,否则将抛异常
-
*
-
* @return
-
*/
-
public static String getPath2() {
-
ResourceBundle rb = ResourceBundle
-
.getBundle( "com/test/modul/utils/prop");
-
path = rb.getString( "path");
-
return path;
-
}
-
-
/**
-
* 三、 使用java.util.PropertyResourceBundle类的构造函数
-
*
-
* @return
-
*/
-
public static String getPath3() {
-
InputStream in;
-
try {
-
in = new BufferedInputStream( new FileInputStream(basePath));
-
ResourceBundle rb = new PropertyResourceBundle(in);
-
path = rb.getString( "path");
-
} catch (FileNotFoundException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
} catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
return path;
-
}
-
-
/**
-
* 四、 使用class变量的getResourceAsStream()方法
-
* 注意:getResourceAsStream()方法的参数按格式写到包路径+properties文件名+.后缀
-
*
-
* @return
-
*/
-
public static String getPath4() {
-
InputStream in = LoadPropertiesFileUtil.class
-
.getResourceAsStream( "/com/test/modul/utils/prop.properties");
-
Properties p = new Properties();
-
try {
-
p.load(in);
-
path = p.getProperty( "path");
-
} catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
return path;
-
}
-
-
/**
-
* 五、
-
* 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
-
* getResourceAsStream(name)方法的参数必须是包路径+文件名+.后缀
-
* 否则会报空指针异常
-
* @return
-
*/
-
public static String getPath5() {
-
InputStream in = LoadPropertiesFileUtil.class.getClassLoader()
-
.getResourceAsStream( "com/test/modul/utils/prop.properties");
-
Properties p = new Properties();
-
try {
-
p.load(in);
-
path = p.getProperty( "path");
-
} catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
return path;
-
}
-
-
/**
-
* 六、 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
-
* getSystemResourceAsStream()方法的参数格式也是有固定要求的
-
*
-
* @return
-
*/
-
public static String getPath6() {
-
InputStream in = ClassLoader
-
.getSystemResourceAsStream( "com/test/modul/utils/prop.properties");
-
Properties p = new Properties();
-
try {
-
p.load(in);
-
path = p.getProperty( "path");
-
} catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
return path;
-
}
-
-
public static void main(String[] args) {
-
System.out.println(LoadPropertiesFileUtil.getPath1());
-
System.out.println(LoadPropertiesFileUtil.getPath2());
-
System.out.println(LoadPropertiesFileUtil.getPath3());
-
System.out.println(LoadPropertiesFileUtil.getPath4());
-
System.out.println(LoadPropertiesFileUtil.getPath5());
-
System.out.println(LoadPropertiesFileUtil.getPath6());
-
}
-
}
-
其中第一、四、五、六种方式都是先获得文件的输入流,然后通过Properties类的load(InputStream inStream)方法加载到Properties对象中,最后通过Properties对象来操作文件内容;
第二、三中方式是通过ResourceBundle类来加载Properties文件,然后ResourceBundle对象来操做properties文件内容。
其中最重要的就是每种方式加载文件时,文件的路径需要按照方法的定义的格式来加载,否则会抛出各种异常,比如空指针异常。