您的位置:首页 > 编程语言 > Java开发

Java读取Properties文件的六种方法

2017-10-12 23:37 459 查看
Java读取Properties文件有以下六种方法:
1。使用java.util.Properties类的load()方法
String fileName="E:/system.properties";
InputStream in = new BufferedInputStream(new FileInputStream(fileName));
Properties p = new Properties();
p.load(in);
System.out.println(p);
2。使用java.util.ResourceBundle类的getBundle()方法
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
ResourceBundle提供软件国际化的捷径,这个类的作用一般是用来读取资源属性文件(properties),然后根据.properties文件的名称信息(本地化信息),匹配当前系统的国别语言信息(也可以程序指定),然后获取相应的properties文件的内容。
注意:,这个properties文件的名字是有规范的:一般的命名规范是: 自定义名_语言代码_国别代码.properties,
如果是默认的,直接写为:自定义名.properties
比如:
myres_en_US.properties
myres_zh_CN.properties
myres.properties
当在中文操作系统下,如果myres_zh_CN.properties、myres.properties两个文件都存在,则优先会使用myres_zh_CN.properties,当myres_zh_CN.properties不存在时候,会使用默认的myres.properties。
例:定义三个资源文件,放到src的根目录下面,:
myres.properties
aaa=good
bbb=thanks

myres_en_US.properties
aaa=good
bbb=thanks

myres_zh_CN.properties
aaa=\u597d
bbb=\u591a\u8c22
import java.util.Locale;
import java.util.ResourceBundle;

public class TestResourceBundle {
public static void main(String[] args) {
Locale locale1 = new Locale("zh", "CN");
ResourceBundle resb1 = ResourceBundle.getBundle("myres", locale1);
System.out.println(resb1.getString("aaa"));

ResourceBundle resb2 = ResourceBundle.getBundle("myres", Locale.getDefault());
System.out.println(resb2.getString("aaa"));

Locale locale3 = new Locale("en", "US");
ResourceBundle resb3 = ResourceBundle.getBundle("myres", locale3);
System.out.println(resb3.getString("aaa"));
}
}
结果:


good

3。使用java.util.PropertyResourceBundle类的构造函数
InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);
PropertyResourceBundle是ResourceBundle的具体子类,是通过对属性文件的静态字符串管理来语言环境资源。与其他资源包类型不同,不能为 PropertyResourceBundle
创建子类。相反,要提供含有资源数据的属性文件。ResourceBundle.getBundle 将自动查找合适的属性文件并创建引用该文件的
PropertyResourceBundle

4。使用java.lang包的class变量的getResourceAsStream()方法
InputStream in = 类名.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
例:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

public class FileGet {
private final static String SYSFILE="/system.properties";

public static void main(String[] args) throws Exception {
File file = new File((FileGet.class.getResource(SYSFILE)).getFile());
InputStream in = new BufferedInputStream(new FileInputStream(file));
Properties p = new Properties();
p.load(in);
System.out.println(p);
InputStream in2 = FileGet.class.getResourceAsStream(SYSFILE);
Properties p2 = new Properties();
p2.load(in2);
System.out.println(p2);
}
}
getResource返回的是java.net包的URL对象,getResourceAsStream返回的是java.io包inputStream
例2:



在上面的目录中,有一个src目录,那么,我们在Test类中应该如何分别获得

其中file.txt可以通过这两种方式来获取:
方法一:
File file1 = new File(Test.class.getResource("file.txt").getFile());
方法二:
File file2 = new File(Test.class.getResource("/com/file.txt").getFile());
file2.txt获取方法:
File file3 = new File(Test.class.getResource("/file2.txt").getFile());
获取不同路径下文件传入的参数不相同。当传入的参数是没有”/”的时候,获取的是当前类所在包下的对应文件。而当参数带有”/”,则是从ClassPath根目录下获取文件。该方法的本质其实只是通过传入path构造一个绝对路径,最终还是由ClassLoader获取资源。

5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例:
InputStream in = 类名.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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java Properties 读取