【java】获取解析资源文件的方法
关于资源文件的读取,有很多种方法,下面补充了多种方法
使用java自带的util包下的ResourceBundle类获取,使用方法最简单
//获取资源文件中键值对
ResourceBundle bundle = ResourceBundle.getBundle("资源文件的名字,不带后缀");
bundle.getString("键名");
注意点:①资源文件直接放在项目的src下,也就是你项目的classpath下,编译完放置的路径在classes下


②注意 ResourceBundle bundle = ResourceBundle.getBundle("quest");这里就填写你资源文件的名字,不用写后缀
③注意 maven管理的项目下,就将配置文件放置在resources文件夹下,编译后的位置就在classes文件夹下

----------------------------------------------------------------------------第二种方法-------------------------------------------------------------------------
2.第二种方法,java.util.Properties获取资源对象
不会因为上面的getString或者getObjec获取不存在的key的时候,会报错的情况


import java.io.IOException;
import java.util.Properties; import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; public class PropertiesRead { @Test
public void testF(){
Properties properties = new Properties();
Resource resource =new ClassPathResource("test_sys.properties");
try {
properties.load(resource.getInputStream());
System.out.println(properties.get("1.4"));
} catch (IOException e) {
System.out.println("异常如下:"+ e);
}
}
}
结果:
不会因为找不到key而报错。
-----------------------------------------------------------------------第三种方法----------------------------------------------------------------------------
3.使用Java自带方式获取文件,区别与第二种方法使用spring工具类获取文件路径、
首先,先看一下怎么能拿到本项目中的资源文件
资源文件内容:
@org.junit.Test
public void test() throws IOException{
System.out.println(this.getClass().getClassLoader().getResource(".").getPath());
}

可以看到 ,使用上面的方法,可以获取到如上的路径,
target是项目编译完之后的编译文件存放的地方,可以在上面的路径下看到咱们想要的资源文件。

可以看到,项目编译完后,编译文件存在的路径是下面
分别对应
classes对应main资源包,test-classes对应test资源包
也就是说,通过上面的方法,就可以获取到每一个模块的跟路径,
那这样就仅使用java自带的方法,获取到项目中的资源文件了。
代码如下:获取资源文件中存在的键
@org.junit.Test
public void test() throws IOException{
FileInputStream fileInputStream = new FileInputStream(this.getClass().getClassLoader().getResource(".").getPath()+"sys_product_price.properties");
Properties properties = new Properties();
properties.load(fileInputStream);
System.out.println(properties.get("1-90"));
}

代码如下:获取不存在的键
@org.junit.Test
public void test() throws IOException{
FileInputStream fileInputStream = new FileInputStream(this.getClass().getClassLoader().getResource(".").getPath()+"sys_product_price.properties");
Properties properties = new Properties();
properties.load(fileInputStream);
System.out.println(properties.get("不存在的键"));
}

---------------------------------------------------------------------------问题----------------------------------------------------------------------------------
【问题】
问题1:在解析资源文件的过程中,

解决方法:
这是因为在本项目的目录下并没有找到本资源文件,把资源文件放到上面展示的位置即可。
-----------------------------------------------------------------------------分割----------------------------------------------------------------------
问题2:
报错如下:
java.util.MissingResourceException: Can't find resource for bundle java.util.PropertyResourceBundle, key 1.4



@Test
public void testF(){
ResourceBundle bundle = ResourceBundle.getBundle("test_sys");
System.out.println(bundle.getString("1.4"));
}
@Test
public void testF(){
ResourceBundle bundle = ResourceBundle.getBundle("test_sys");
System.out.println(bundle.getObject("1.4").toString());
}
不论是getString(键名)还是getObject(键名),如果找不到键名,都不会返回Null,而是会报错。
问题原因:
找不到键名“1.4”.

解决方法:
@Test
public void testF(){
ResourceBundle bundle = ResourceBundle.getBundle("test_sys");
try {
System.out.println(bundle.getString("1.4"));
} catch (MissingResourceException e) {
System.out.println("捕捉异常,自行处理,不至于程序报错");
}
}
或者,使用文章开头的 第二种Properties方式。
------------------------------------------------------------------分割--------------------------------------------------------------------
【java】获取解析资源文件的方法的更多相关文章
- java中读取资源文件的方法
展开全部 1.使用java.util.Properties类的load()方法 示例: //文件在项目下.不是在包下!! InputStream in = new BufferedInputStrea ...
- java读取项目资源文件的方法
1.把资源文件放在项目的Resource文件夹下,并使其设置成为资源文件夹(通过idea或者eclise)2. Thread.currentThread().getContextClassLoader ...
- java 中获得 资源文件方法
1 java 中获取资源文件的方法 项目目录如下 获取当前项目的目录路径 方法一:使用类名 MergeDocHandler.class.getClassLoader().getResource(&qu ...
- java后台获取国际化资源文件
//current属性,由于此属性只做赋值操作,不做取值操作,因此没有get方法 private Locale current; public void setCurrent(Locale cur) ...
- Java加载资源文件的两种方法
处理配置文件对于Java程序员来说再常见不过了,不管是Servlet,Spring,抑或是Structs,都需要与配置文件打交道.Java将配置文件当作一种资源(resource)来处理,并且提供了两 ...
- Java解析JSON文件的方法
http://blog.sina.com.cn/s/blog_628cc2b70101dydc.html java读取文件的方法 http://www.cnblogs.com/lovebread/ar ...
- Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件
写在前面 从大四实习至今已一年有余,作为一个程序员,一直没有用心去记录自己工作中遇到的问题,甚是惭愧,打算从今日起开始养成写博客的习惯.作为一名java开发人员,Spring是永远绕不过的话题,它的设 ...
- java 获取classpath下文件多种方式
java 获取classpath下文件多种方式 一:properties下配置 在resources下定义server.properties register.jks.path=classpath\: ...
- Android程序解析XML文件的方法及使用PULL解析XML案例
一.一般解析XML文件的方法有SAX和DOM.PULL (1)DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准.DOM是以层次结构组织的节点或信 ...
随机推荐
- 【Foreign】采蘑菇 [点分治]
采蘑菇 Time Limit: 20 Sec Memory Limit: 256 MB Description Input Output Sample Input 5 1 2 3 2 3 1 2 1 ...
- 【BZOJ2049】【SDOI2008】洞穴勘测 [LCT]
洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MB[Submit][Status][Discuss] Description 辉辉热衷于洞穴勘测.某天,他按照地 ...
- bzoj2453/2120 数颜色
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2120 http://www.lydsy.com/JudgeOnline/problem.ph ...
- Linux : 从私钥中提取公钥
已知一个私钥, 如何从其中提取公钥出来? 提取公钥 ssh-keygen -y -f /path/to/private_key > /path/to/public_key
- spin_lock浅析【转】
转自:http://blog.csdn.net/frankyzhangc/article/details/6569475 版权声明:本文为博主原创文章,未经博主允许不得转载. 今天我们详细了解一下sp ...
- ubuntu下wireshark+scapy+tcpreply
安装wireshark命令: sudo apt-get install wireshark 运行打开wireshark命令: sudo wireshark(一定要以超级权限打开) 正确打开的窗口应该默 ...
- onchange监听input值变化及input隐藏后change事件不触发的原因与解决方法(设置readonly后onchange不起作用的解决方案)
转自:https://www.cnblogs.com/white0710/p/7338456.html 1. onchange事件监听input值变化的使用方法: <input id=" ...
- Selenium2+python自动化40-cookie相关操作【转载】
前言 虽然cookie相关操作在平常ui自动化中用得少,偶尔也会用到,比如登录有图形验证码,可以通过绕过验证码方式,添加cookie方法登录. 登录后换账号登录时候,也可作为后置条件去删除cookie ...
- PHP7中php.ini、php-fpm和www.conf的配置(转)
根据前文 <2015博客升级记(五):CentOS 7.1编译安装PHP7> 的 configure 编译参数设定,安装后的PHP7配置文件所在路径是 /usr/local/php7/et ...
- [BZOJ4553][Tjoi2016&Heoi2016]序列 cdp分治+dp
4553: [Tjoi2016&Heoi2016]序列 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 260 Solved: 133[Sub ...