简单的列出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 ************
张国明
分享到:
相关推荐
Java读取属性文件,可以参考参考。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
java读取pdf文件作者、标题等属性
java读取tiff文件,并获得长,高。
读取自定义属性文件内容,可以自己定义属性文件的名字,任何路径都可以
读取shp文件的代码,也可以读DBF属性
shp文件:地理信息系统,也被称作GIS,它主要的扩展类型是SHAPEFILE (.SHP),一个包含了矢量地理空间数据的流行文件格式,描述了几何形态,点,线和多边形...该方法通过java代码实现将shp文件的数据读取以及存入数据库
自己做的用java读取kml文件数据,并保存为json文件。方便其它地方解析。
java读取xls中的数据,每个对象对应多个属性值,根据每个对象的属性值来生成一个二维矩阵
以下是对在JAVA中读取属性文件的几种方法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助
JAVA配置文件读取,加载操作
* 得到属性文件的值 */ public String getValueByPropertyKey(String key) { String value = null; if (checkPropertyFile()) { value = getValue(key); } return value; } /* * get Value ...
GetFlvInfo类提供读取文件属性的方法;很方便
通过单例模式实例化获取propertyUtil 工具包实例,高效加载配置文件,java语言编写。通过单例模式实例化获取propertyUtil 工具包实例,高效加载配置文件,java语言编写。通过单例模式实例化获取propertyUtil 工具包...
用WC3的DOM实现对XML文件的读取。 方法摘要: //通过文件路径及文件名读取xml public XMLReader(String path, String filename); //初始化读取包下的的XML文件 public XMLReader(Package package1, String fileName)...
* 属性文件全名 */ private static final String PFILE ="Config.properties"; /** * 对应于属性文件的文件对象变量 */ private File m_file = null; /** * 属性文件的最后修改日期 */ private...
JAVA取得可执行程序、文件的版本号 JAVA取得exe、dll文件的版本号
JavaJNDI 数据链接池及 属性文件读取数据链接信息