【java】获取解析资源文件的方法

关于资源文件的读取,有很多种方法,下面补充了多种方法

1.java.util.ResourceBundle

使用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:在解析资源文件的过程中,

 java.util.MissingResourceException: Can't find bundle for base name quest, locale zh_CN 
 
 

 

解决方法:

这是因为在本项目的目录下并没有找到本资源文件,把资源文件放到上面展示的位置即可。

 

 

-----------------------------------------------------------------------------分割----------------------------------------------------------------------

问题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方式。

 

------------------------------------------------------------------分割--------------------------------------------------------------------

posted @ 2017-06-27 14:37  Angel挤一挤  阅读(1020)  评论(0编辑  收藏  举报