Java读取Properties文件的六种方法
Java读取Properties文件有以下六种方法:
1。使用java.util.Properties类的load()方法
StringfileName="E:/system.properties";InputStreamin=newBufferedInputStream(newFileInputStream(fileName));Propertiesp=newProperties();p.load(in);System.out.println(p);
2。使用java.util.ResourceBundle类的getBundle()方法
ResourceBundlerb=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
importjava.util.Locale;importjava.util.ResourceBundle;publicclassTestResourceBundle{publicstaticvoidmain(String[]args){Localelocale1=newLocale("zh","CN");ResourceBundleresb1=ResourceBundle.getBundle("myres",locale1);System.out.println(resb1.getString("aaa"));ResourceBundleresb2=ResourceBundle.getBundle("myres",Locale.getDefault());System.out.println(resb2.getString("aaa"));Localelocale3=newLocale("en","US");ResourceBundleresb3=ResourceBundle.getBundle("myres",locale3);System.out.println(resb3.getString("aaa"));}}
结果:
好
好
good
3。使用java.util.PropertyResourceBundle类的构造函数
InputStreamin=newBufferedInputStream(newFileInputStream(name));ResourceBundlerb=newPropertyResourceBundle(in);
PropertyResourceBundle是ResourceBundle的具体子类,是通过对属性文件的静态字符串管理来语言环境资源。与其他资源包类型不同,不能为 PropertyResourceBundle 创建子类。相反,要提供含有资源数据的属性文件。ResourceBundle.getBundle 将自动查找合适的属性文件并创建引用该文件的 PropertyResourceBundle
4。使用java.lang包的class变量的getResourceAsStream()方法
InputStreamin=类名.class.getResourceAsStream(name);Propertiesp=newProperties();p.load(in);
例:
importjava.io.BufferedInputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.InputStream;importjava.util.Properties;publicclassFileGet{privatefinalstaticStringSYSFILE="/system.properties";publicstaticvoidmain(String[]args)throwsException{Filefile=newFile((FileGet.class.getResource(SYSFILE)).getFile());InputStreamin=newBufferedInputStream(newFileInputStream(file));Propertiesp=newProperties();p.load(in);System.out.println(p);InputStreamin2=FileGet.class.getResourceAsStream(SYSFILE);Propertiesp2=newProperties();p2.load(in2);System.out.println(p2);}}
getResource返回的是java.net包的URL对象,getResourceAsStream返回的是java.io包inputStream
例2:
在上面的目录中,有一个src目录,那么,我们在Test类中应该如何分别获得
其中file.txt可以通过这两种方式来获取:
方法一:
Filefile1=newFile(Test.class.getResource("file.txt").getFile());
方法二:
Filefile2=newFile(Test.class.getResource("/com/file.txt").getFile());
file2.txt获取方法:
Filefile3=newFile(Test.class.getResource("/file2.txt").getFile());
获取不同路径下文件传入的参数不相同。当传入的参数是没有”/”的时候,获取的是当前类所在包下的对应文件。而当参数带有”/”,则是从ClassPath根目录下获取文件。该方法的本质其实只是通过传入path构造一个绝对路径,最终还是由ClassLoader获取资源。
5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例:
InputStreamin=类名.class.getClassLoader().getResourceAsStream(name);Propertiesp=newProperties();p.load(in);
6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例:
InputStreamin=ClassLoader.getSystemResourceAsStream(name);Propertiesp=newProperties();p.load(in);
补充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:
InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。