简单的列出java读取属性文件的几种方法,测试代码如下:
test.properties文件的内容就一行,如下:
name=张国明
执行结果如下:
************ 1 ************
张国明
没有这个key,这是默认值。
************ 2 ************
张国明
************ 3 ************
张国明
************ 4 ************
张国明
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
/**
* 测试类
*
* @author 张国明 guomingzhang2008@gmail.com <br/>
* 2012-8-19 下午10:14:01
*
*/
public class Test {
/**
* @param args
* arg
* @throws IOException
* ioe
*/
public static void main(final String[] args) throws IOException {
/**
* 属性文件的绝对路径
*/
final String absolutelyClasspath = "/com/hqsoft/test.properties";
/**
* 属性文件的相对路径
*/
final String relativeClasspath = "com/hqsoft/test.properties";
/**
* 属性文件的屋里路径路径
*/
final String filePath = "E:/works_eclipse/hqsoft/test/com/hqsoft/test.properties";
final Properties p1 = load1ByAbsFilePath(filePath);
System.out.println("************ 1 ************");
System.out.println(p1.getProperty("name"));
System.out.println(p1.getProperty("name111", "没有这个key,这是默认值。"));
final ResourceBundle rb2 = load2ByAbsFilePath(filePath);
System.out.println("************ 2 ************");
System.out.println(rb2.getString("name"));
final Properties p3 = load3ByAbsClasspath(absolutelyClasspath);
System.out.println("************ 3 ************");
System.out.println(p3.getProperty("name"));
final Properties p4 = load4ByRelClasspath(relativeClasspath);
System.out.println("************ 4 ************");
System.out.println(p4.getProperty("name"));
// 补充: Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
// final InputStream in = context.getResourceAsStream(path);
// final Properties p = new Properties();
// p.load(in);
// 在servlet中,读到的 文件路径 要为 "/WEB-INF" 。
}
/**
* 使用java.util.Properties类的load()方法
*
* @param filePath
* 文件的物理路径
* @return
* @throws IOException
*/
private static Properties load1ByAbsFilePath(final String filePath)
throws IOException {
final InputStream is = new BufferedInputStream(new FileInputStream(filePath));
final Properties properties = new Properties();
properties.load(is);
return properties;
}
/**
* 使用java.util.PropertyResourceBundle类的构造函数
*
* @param filePath
* 文件的物理路径
* @return
* @throws FileNotFoundException
* @throws IOException
*/
private static ResourceBundle load2ByAbsFilePath(final String filePath)
throws FileNotFoundException, IOException {
final InputStream is = new BufferedInputStream(new FileInputStream(filePath));
return new PropertyResourceBundle(is);
}
/**
* 使用java.util.Properties.class变量的getResourceAsStream()方法
*
* @param absClasspath
* classpath的绝对路径
* @return
* @throws IOException
*/
private static Properties load3ByAbsClasspath(final String absClasspath)
throws IOException {
final InputStream is4 = Properties.class .getResourceAsStream(absClasspath);
final Properties p4 = new Properties();
p4.load(is4);
return p4;
}
/**
* 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
*
* @param relClasspath
* class的相对路径
* @return
* @throws IOException
*/
private static Properties load4ByRelClasspath(final String relClasspath)
throws IOException {
final InputStream is = ClassLoader.getSystemResourceAsStream(relClasspath);
final Properties properties = new Properties();
properties.load(is);
return properties;
}
}
test.properties文件的内容就一行,如下:
name=张国明
执行结果如下:
************ 1 ************
张国明
没有这个key,这是默认值。
************ 2 ************
张国明
************ 3 ************
张国明
************ 4 ************
张国明