使用J2SE
API读取Properties文件的六种方法,//如果配置文件在包中,要加入配置文件的路径,一般是在classpath下依次查找,如conf
/crm/db.properties,默认路径在classpath下,如果配置文件就在classpath下,没有放在包中,就直接写文件名。
1。使用java.util.Properties类的load()方法
示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);
2。使用java.util.ResourceBundle类的getBundle()方法
示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
3。使用java.util.PropertyResourceBundle类的构造函数
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);
4。使用class变量的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in); //如果配置文件在包中,要加入配置文件的路径,一般是在classpath下依次查找。如conf/crm/db.properties
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文件的工具类,传入配置文件名字和其中的key就可以读取
java如何读取properties配置文件
利用Java的Properties 类读取配置文件信息! 读取Properties文件的例子
主要介绍了java读取properties配置文件的方法,涉及java操作properties配置文件的相关技巧,需要的朋友可以参考下
java项目读取配置文件的实例代码,使用class.getClassLoader().getResourceAsStream方法。
读取properties文件返回map
NULL 博文链接:https://z-gxjs.iteye.com/blog/1846423
NULL 博文链接:https://bijian1013.iteye.com/blog/2307358
使用Java中的Properties类的方法,以及类加载器来读取配置文件中的信息。可以读取.properties文件和.cfg文件。详细代码+注释+测试例子
NULL 博文链接:https://liuzidong.iteye.com/blog/776637
大家都喜欢把配置文件放在src目录下,如果有10个以上的配置文件为什么不考虑在WEB-INF目录下新建一个文件夹,专门放配置文件;这样即好管理,文件安全性又高。亲问题已经解决,把源代码共享给大家,已经通过测试;...
主要介绍了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的内容
public class CommonParam { private String propertyFileName; private ResourceBundle resourceBundle; public CommonParam() { ... resourceBundle = ResourceBundle.getBundle(propertyFileName);...
Java配置文件读取,采用单例模式加载config.properties
像Python支持的配置文件是.ini文件,同样,它也有自己读取配置文件的类ConfigParse,方便程序员或用户通过该类的方法来修改.ini配置文件。在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的...
自己封装的JDBC连接通过properties文件读取配置文件
通常我们的做法是用配置文件来解决。 各种语言都有自己所支持的配置文件类型。比如Python ,他支持.ini 文件。因为他内部有一个ConfigParser 类来支持.ini 文件的读写,根据该类提供的方法程序员可以自由的来操作....
java中properties文件读取demo