java读取属性文件

简单的列出java读取属性文件的几种方法,测试代码如下:


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 ************
张国明
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值