欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java读取Properties文件几种方法总结

时间:2021-03-01 13:43:57|栏目:JAVA代码|点击:

使用J2SE API读取Properties文件的六种方法

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);

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 selenium截图操作的实现

栏    目:JAVA代码

下一篇:Spring如何解决循环依赖的问题

本文标题:Java读取Properties文件几种方法总结

本文地址:http://www.codeinn.net/misctech/72232.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有