用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同级。
未知来源
转载于:https://www.cnblogs.com/davidwang456/p/3782057.html
用java读取properties文件--转相关推荐
- Java基础学习总结(15)——java读取properties文件总结
2019独角兽企业重金招聘Python工程师标准>>> 一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都 ...
- java读取.properties文件及解决中文乱码问题
java读取.properties文件及解决中文乱码问题 参考文章: (1)java读取.properties文件及解决中文乱码问题 (2)https://www.cnblogs.com/helloq ...
- java读取properties文件_Java读取Properties文件的六种方法
Java读取Properties文件有以下六种方法: 1.使用java.util.Properties类的load()方法String fileName="E:/system.propert ...
- JAVA读取Properties文件对象常用方法总结
实际开发中,总是会需要用到配置文件的,常用的就是properties.xml.json.比如,使用jdbc访问数据库时,我们就可以将driver.url.username.password这几个参数记 ...
- java读取Properties文件及赋值
懒得整理了,直接复制我工程里的代码: 第一个读取文件的类可以直接复制, 不复制的话,在下面取值和赋值部分的代码new EmailProperRead(systemType).properties根据自 ...
- Java读取Properties文件的六种方法
1.使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInputStream(new FileInputStream ...
- java读取properties文件_java读取properties文件的几种方法
一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取 1 Properties p=newProperties() ...
- java 读取properties文件
1.不在项目中读取 Properties properties = new Properties(); BufferedReader read = new Buffer ...
- java读取.properties文件乱码
1.config.properties文件写不进中文,写进去都变成了unicode,解决的方法是右键该文件--Properties--Resource--Text file encoding ,选ot ...
最新文章
- 1050. 螺旋矩阵(25)
- MVC4.0网站发布和部署到IIS7.0上的方法【转:http://www.th7.cn/Program/net/201403/183756.shtml】...
- Python判断一个字符串是否包含某个指定的字符串
- request.getContextPath()的功能
- SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件
- kafka_2.10-0.8.1.1.tgz的1或3节点集群的下载、安装和配置(图文详细教程)绝对干货...
- 用c语言验证装载问题 回溯法,《算法分析与设计》期末考试复习题纲(完整版)...
- centos6.5编译安装mysql_Centos6.5编译安装mysql 5.7.14详细教程
- 吴裕雄--天生自然C语言开发:作用域规则
- WebTrends 更强大、更灵活的网站分析工具
- 简洁实用jQuery进度条插件
- apt-get 提示 无法解析域名“cn.archive.ubuntu.com” 的解决
- 有多厉害?首互联网卫星发射 飞机高铁WiFi就靠它了
- Typora + PicGo + 七牛云图床
- Springboot @Lazy注解
- 为揭秘CSDN谁有100万粉丝?我连夜研发了粉丝数排行榜插件,通过 dalao 一键即可唤醒
- git 拉取指定远程分支的内容
- Ubuntu 安装Jdk1.8
- linux 系统 JAVA_HOME is not set问题解决
- 解决KEIL MDK中JLINK烧录代码时提示the connected jlink is defective问题
热门文章
- python函数能否增强代码可读性_总结的几个Python函数方法设计原则
- jpa mysql乐观锁_JPA @Lock(value = LockModeType.PESSIMISTIC_WRITE) 悲观锁防坑
- 之江汇空间如何加音乐背景_从脚本到成品,教你如何快速制作出创意又浪漫的婚礼视频...
- oracle获取堆栈,如何从RAISED异常中获取oracle PL / SQL中原始异常的堆栈跟踪?
- 风暴航路的地图编号_砖厂风暴航路已倒闭,新的搬砖圣地诞生
- Qt中应用程序的打包与发布
- android ip rule 策略路由,ip rule 策略路由
- java equals() 函数_java equals()函数与‘==’
- 华为云查询弹性云服务器规格信息,通用计算型弹性云服务器规格介绍详情-华为云...
- 随时找到数据流中的中位数