编程之道

资源文件的路径与读取

2018-05-09  本文已影响0人  7叶

一、文件目录打印图

.
|-- java
|   |-- hardwork
|   |   |-- equalsTest
|   |   |   `-- TestEquals.java
|   |   `-- getResourceTest
|   |       `-- TestGetResource.java
`-- resources
    |-- database.properties
    `-- hardwork
        |-- getResourceTest
        |   `-- siblingFile.properties
        `-- resourceFile
            `-- other.properties


二、properties文件介绍

以下内容引自Wikipedia:

.properties 文件是一种和java相关的文件拓展,用于存储配置信息。每个参数以一对字符串的信息存储,即key-value 对。

属性信息的格式:

可以是:key=valuekey = valuekey:valuekey value

#! 用于注释该行(当其在属性中时,将不起作用),\ 用于转义和拼接多行的value

模板:

# You are reading the ".properties" entry.
! The exclamation mark can also mark text as comments.
# The key characters =, and : should be written with
# a preceding backslash to ensure that they are properly loaded.
# However, there is no need to precede the value characters =, and : by a backslash.
website = https://en.wikipedia.org/
language = English
# The backslash below tells the application to continue reading
# the value onto the next line.
message = Welcome to \
          Wikipedia!
# Add spaces to the key
key\ with\ spaces = This is the value that could be looked up with the key "key with spaces".
# Unicode
tab : \u0009
# If you want your property to include a backslash, it should be escaped by another backslash
path=c:\\wiki\\templates
# However, some editors will handle this automatically

三、获取文件路径

注意: 这里获取文件路径,和读取文件的一个最高级范围的限定。

在java项目和web项目中,其最高级的目录只能是并行的java 目录和resource 目录。

因此,我们只能操作java 的源代码文件和resource 的资源文件。对于web项目来说,我们是无法通过这里讲的方法来获取webapp 目录下的文件的。

我们想获得的文件路径,无非是两种。一是java类文件的路径(*.java ),二是资源文件的路径(*.properties 等)。通常情况下,我们主要是获取资源文件的路径。

这里我们使用TestGetResource.java 类来获取siblingFile.propertiesother.properties 这两个文件的路径。

说明: 下面所讲的方法,其定位参照的方法都是借助.class 类文件来展开的,因此其位置都是编译后的文件位置(当然,通常其位置和源代码位置一致)。

我们所获取的文件路径,都是绝对路径(相对于系统而言的全写路径)。

1、URL <- Concrete.class.getResource(String path)方法

在这里,path可以是相对路径,也可以是绝对路径(绝对路径的path以/ 开头,指向你程序的根目录)。得到的结果是URL 类型。

1.1 path使用相对路径

当使用相对路径时,其参照目录是当前类文件所在的目录。当path传入的值为"" 时,获取的就是当前目录的路径。

URL url_1 = TestGetResource.class.getResource("siblingFile.properties");
File file_1 = new File(url_1.getFile());
String filepath_1 = file_1.toPath();

1.2 path使用绝对路径

当使用绝对路径时,必须是以/ 开头,这代表了当前java源代码的根目录。当path传入的值为/ 时,获取的就是java源代码的根目录。

URL url_2 = TestGetResource.class.getResource("/hardwork/resourceFile/other.properties");
File file_2 = new File(url_2.getFile());
String filepath_2 = file_2.toPath();

当要获取的资源文件路径比较复杂时,就只能用绝对路径了。

2、URL <- Concrete.class.getClassLoader().getResource(String path)方法

在这里,通过获取类加载器来获取资源文件的路径。path只能是绝对路径。其实介绍的第一种方法,其内部就是包含了调用这里的第二种方法。

URL url_3 = TestGetResource.class.getClassLoader().getResource("/hardwork/getResourceTest/siblingFile.properties");
File file_3 = new File(url_3.getFile());
String filepath_3 = file_3.toPath();

总结

上面的方法都是用来获取文件的路径,除了得到String 的路径值,在获取过程中,我们还可以得到资源文件的File 对象。而File 对象在后面的资源文件的读取中就会使用到。

四、读取资源文件

更多的时候,我们手续要读取资源文件内部的信息。对于.properties 文件(一般是键值对的形式保存信息),在java 中有一个与其对应的类,即Properties 类。通过构造Properties 类,我们可以读取资源的keyvalue

1、 Properties对象读取资源文件

1.1 生成properties 对象

首先,我们需要生成Properties 对象:Properties properties= new Properties();

1.2 获取资源文件的输入流

然后,我们需要得到资源文件的输入流,而得到输入流的方法有两种。

1.2.1 Concrete.class.getResourceAsStream(String path)

path可以是相对路径,也可以是绝对路径。

InputStream in = TestGetResource.class.getResourceAsStream("siblingFile.properties");
1.2.2 Concrete.class.getClassLoader().getResourceAsStream(String path)

path只能是绝对路径。且绝对路径不以/ 开头。

InputStream in = TestGetResource.class.getClassLoader().getResourceAsStream("hardwork/resourceFile/other.properties");

1.3 加载输入流,获取值

在得到输入流之后,将输入流加载到Properties 对象,之后就可以获取值了。

properties.load(in);
String value = properties.getProperty("name");

2、ResourceBundle对象读取资源文件

这种方法只能传入绝对路径,且绝对路径不以/ 开头。

ResourceBundle bundle = ResourceBundle.getBundle();
String value = bundle.getString("name");
上一篇 下一篇

猜你喜欢

热点阅读