根据网上的例子总结了一下。
其中cache.properties放到src下,也可以放到WEB-INF下
4.package test.bwl;
5.
6. import java.io.FileNotFoundException;
7. import java.io.IOException;
8. import java.io.InputStream;
9. import java.util.Properties;
10.
11.public class Test {
12. private static Properties properties = new Properties();
13.
14. public static void main(String[] args) {
15. try {
16. InputStream is = Test. class .getClassLoader().getResourceAsStream( "cache.properties" );
17. properties.load(is);
18. String size = properties.getProperty( "cache.size" );
19. writeLog( "配置成功!" + size);
20. } catch (FileNotFoundException e) {
21. writeLog( "配置文件不存在!" + e.getMessage());
22. } catch (IOException e) {
23. writeLog( "读取配置文件IO错误!" + e.getMessage());
24. }
25. }
26.
27. public static void writeLog(String strLog) {
28. System.out.println(strLog);
29. }
30.}
1.使用java.util.ResourceBundle类的getBundle()方法
示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
return rb.getString("key");
注:该方法可以读jar包里的文件
2.使用java.util.PropertyResourceBundle类的构造函数
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);
return rb.getString("key");
3.使用java.util.Properties类的load()方法
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);
return p.getProperties("key"); or return p.getProperties("key", "defaultValue");
4.使用class变量的getResourceAsStream()方法
示例: InputStream in = ClassName.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
return p.getProperties("key"); or return p.getProperties("key", "defaultValue");
5.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例: InputStream in = ClassName.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
return p.getProperties("key"); or return p.getProperties("key", "defaultValue");
6.使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例: InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);
return p.getProperties("key"); or return p.getProperties("key", "defaultValue");
补充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
return p.getProperties("key"); or return p.getProperties("key", "defaultValue");
7.读jar外面的文件
FileInputStream fis;
Properties p = new Properties();
fis = new FileInputStream(initfileName);
prop.load(fis);
注:jar文件执行时,注意cmd路径必须与jar路径相同
分享到:
相关推荐
java如何读取properties配置文件
大家都喜欢把配置文件放在src目录下,如果有10个以上的配置文件为什么不考虑在WEB-INF目录下新建一个文件夹,专门放配置文件;这样即好管理,文件安全性又高。亲问题已经解决,把源代码共享给大家,已经通过测试;...
NULL 博文链接:https://liuzidong.iteye.com/blog/776637
java读取properties文件的工具类,传入配置文件名字和其中的key就可以读取
利用Java的Properties 类读取配置文件信息! 读取Properties文件的例子
主要介绍了java读取properties配置文件的方法,涉及java操作properties配置文件的相关技巧,需要的朋友可以参考下
NULL 博文链接:https://jeemygrow.iteye.com/blog/1162827
ConfigFile configfile = ConfigFile.getInstance("ipConfig123.properties"); String ip = configfile.getkeyvalue("ip"); 可以取出ipConfig123.properties 文件中IP的内容
主要介绍了java简单读取properties配置文件的方法,涉及java针对properties配置的载入及文件属性读取相关操作技巧,需要的朋友可以参考下
NULL 博文链接:https://bijian1013.iteye.com/blog/2307358
下面小编就为大家带来一篇Java读取properties配置文件时,出现中文乱码的解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
properties文件获取工具类:静态加载properties配置文件,有根据key获取值的方法
读取properties文件返回map
使用Java中的Properties类的方法,以及类加载器来读取配置文件中的信息。可以读取.properties文件和.cfg文件。详细代码+注释+测试例子
【Java 语言】读取 properties 配置文件 ( Java 语言中的 properties 配置文件 | 使用 properties 配置文件 ) https://hanshuliang.blog.csdn.net/article/details/134435350 博客源码
java中properties文件读取demo
通常我们的做法是用配置文件来解决。 各种语言都有自己所支持的配置文件类型。比如Python ,他支持.ini 文件。因为他内部有一个ConfigParser 类来支持.ini 文件的读写,根据该类提供的方法程序员可以自由的来操作....
主要介绍了Java读取properties配置文件的方法讲解,properties可以被看作是Java世界的ini,Java中有Properties可以操作它,需要的朋友可以参考下
Java配置文件读取,采用单例模式加载config.properties
主要介绍了Java 读取、获取配置文件.properties中的数据,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下