用java读取properties文件--转
今天为了通过java读取properties文件,google了很长时间,终于找到了。现在特记录之和大家一起分享。
下面直接贴出代码:java类
public class Mytest
public static void readFile(String fileName) {//传入参数fileName是要读取的资源文件的文件名如(file.properties)
InputStream in = null;
Properties pros = new Properties();
try {
if (null != fileName) {
//前提是资源文件必须和Mytest类在同一个包下
in = Mytest.class.getResourceAsStream(fileName);
//得到当前类的路径,并把资源文件名作为输入流
pros.load(in);
Enumeration en = pros.propertyNames();//得到资源文件中的所有key值
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
System.out.println("key=" + key + " value=" + pros.getProperty(key));
//输出资源文件中的key与value值
}
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("读取资源文件出错");
} finally {
try {
if (null != in) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭流失败");
}
} }
方法二:
import java.util.MissingResourceException;
import java.util.ResourceBundle; public class Messages {
private static final String BUNDLE_NAME = "com.xxx.cs.mm.service.messages"; //messages.properties文件和Messages类在同一个包下,包名:com.xxx.cs.mm.service private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME); private Messages() {
} public static String getString(String key) {
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
}
转自:http://duqiangcise.iteye.com/blog/319793
使用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);
注意:
this.getclass.getResourceAsStream(name); //类与资源文件同级--同一个目录下
this.class.getClassLoader().getResourceAsStream(name); //资源文件与classpath同级。
未知来源
用java读取properties文件--转的更多相关文章
- java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)
java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...
- java 读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java基础学习总结——java读取properties文件总结
摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...
- java读取properties文件时候要注意的地方
java读取properties文件时,一定要注意properties里面后面出现的空格! 比如:filepath = /home/cps/ 我找了半天,系统一直提示,没有这个路径,可是确实是存在的, ...
- java基础—java读取properties文件
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- Java基础学习总结(15)——java读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java读取.properties文件
在web开发过程中,有些配置要保存到properties文件里,本章将给出一个工具类,用来方便读取properties文件. 案例: 1:config.properties文件 name=\u843D ...
- Java 读取Properties文件时应注意的路径问题
1. 使用Class的getResourceAsStream()方法读取Properties文件(资源文件)的路径问题: InputStream in = this.getClass().getRe ...
随机推荐
- 排名第一、第二的OCR软件
排名第一.第二的OCR软件 第一:ABBYY FineReader OCR世界排名第一,在俄罗斯获国际科技大奖奖超过卡巴斯基! 不仅仅只是文字识别,还能表格识别,版面还原,字体识别,文档结构 ...
- bzoj 2852: 强大的区间 辗转相除
2852: 强大的区间 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 45 Solved: 12[Submit][Status][Discuss] D ...
- Django 安全策略的 7 条总结!
Florian Apolloner 发言主题为 Django 安全,其中并未讨论针对 SSL 协议的攻击--因为那不在 Django 涉及范围内.(如感兴趣可参考 https://www.ssllab ...
- 纯js将form表单的数据封装成json 以便于ajax发送
使用方式: var json = form2Json("formId");//这里的参数是form表单的id值 form2json.js function form2Json(fo ...
- [wikioi]奇怪的梦境
http://wikioi.com/problem/2833/ 拓扑排序,居然1A,哈哈. #include <cstdio> #include <iostream> #inc ...
- 17.1.1 How to Set Up Replication 设置复制:
17.1.1 How to Set Up Replication 设置复制: 17.1.1.1 Setting the Replication Master Configuration 17.1.1. ...
- WordPress Lazy SEO插件lazyseo.php脚本任意文件上传漏洞
漏洞名称: WordPress Lazy SEO插件lazyseo.php脚本任意文件上传漏洞 CNNVD编号: CNNVD-201309-446 发布时间: 2013-09-26 更新时间: 201 ...
- Oracle和MSSQL查询有多少张表
Oracle: SELECT count(*) FROM user_tables MSSQL: ) FROM sysobjects WHERE xtype='U' 这种方法可能会把dbo.dtprop ...
- 关于.NET三层 分类: C#
三层体系结构的概念 用户界面表示层(USL) 业务逻辑层(BLL) 数据访问层(DAL) BLL将USL与DAL隔开了,并且加入了业务规则 各层的作用 1:数据数据访问层:主要是对原始数据(数据库或者 ...
- JavaScript高级程序设计26.pdf
DOM操作技术 动态脚本 指得是页面加载时不存在,但将来的某一时刻通过修改DOM动态添加的脚本,跟操作HTML元素一样,创建动态脚本也有2种方式:插入外部文件和直接插入JavaScript代码 var ...