读取resources下的资源

这里我通过一个普通的SpringBoot项目进行测试,当然其他项目也都是通用的。

将其中的Test修改为你的类名即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
 
 
import java.io.*;
import java.net.URLDecoder;
import java.util.Objects;
 
/**
 * @ClassName Test
 * @Author zhangzhixi
 * @Description
 * @Date 2022-8-11 16:48
 * @Version 1.0
 */
@Slf4j
public class Test {
    public static void main(String[] args) throws IOException {
        log.info("\n==============测试读取resources下的资源==============");
        String fileName = "application.yml";
        log.info("\n==============第一版==============");
        function1(fileName);
        log.info("\n==============第二版==============");
        function2(fileName);
        log.info("\n==============第三版==============");
        function3(fileName);
        log.info("\n==============第四版==============");
        function4(fileName);
        log.info("\n==============第五版==============");
        function5(fileName);
        log.info("\n==============第六版==============");
        function6(fileName);
        log.info("\n==============第七版==============");
        function7(fileName);
    }
 
    /**
     * 通过类路径找到resources下的资源
     *
     * @param fileName 资源名称
     * @throws IOException 异常
     */
    private static void function1(String fileName) throws IOException {
        String path = Objects.requireNonNull(Test.class.getClassLoader().getResource("")).getPath();
        System.out.println(path);
        String filePath = path + fileName;
        System.out.println(filePath);
        getFileContent(filePath);
    }
 
    /**
     * 直接通过文件名getPath来获取路径
     *
     * @param fileName 文件名
     * @throws IOException IO异常
     */
    public static void function2(String fileName) throws IOException {
        String path = Objects.requireNonNull(Test.class.getClassLoader().getResource(fileName)).getPath();
        System.out.println(path);
        //如果路径中带有中文会被URLEncoder,因此这里需要解码
        String filePath = URLDecoder.decode(path, "UTF-8");
        System.out.println(filePath);
        getFileContent(filePath);
    }
 
    /**
     * 直接使用getResourceAsStream方法获取流
     * springboot项目中需要使用此种方法,因为jar包中没有一个实际的路径存放文件
     *
     * @param fileName 文件名
     * @throws IOException IO异常
     */
    public static void function3(String fileName) throws IOException {
        InputStream in = Test.class.getClassLoader().getResourceAsStream(fileName);
        getFileContent(in);
    }
 
    /**
     * 直接使用getResourceAsStream方法获取流
     * 如果不使用getClassLoader,可以使用getResourceAsStream("/配置测试.txt")直接从resources根路径下获取
     *
     * @param fileName 文件名
     * @throws IOException IO异常
     */
    public static void function4(String fileName) throws IOException {
        InputStream in = Test.class.getResourceAsStream("/" + fileName);
        getFileContent(in);
    }
 
    /**
     * 通过ClassPathResource类获取,建议SpringBoot中使用
     * springboot项目中需要使用此种方法,因为jar包中没有一个实际的路径存放文件
     *
     * @param fileName 文件名
     * @throws IOException IO异常
     */
    public static void function5(String fileName) throws IOException {
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        InputStream inputStream = classPathResource.getInputStream();
        getFileContent(inputStream);
    }
 
    /**
     * 通过绝对路径获取项目中文件的位置(不能用于服务器)
     *
     * @param fileName 文件名
     * @throws IOException IO异常
     */
    public static void function6(String fileName) throws IOException {
        // 获取项目的绝对路径
        String rootPath = System.getProperty("user.dir");
        String filePath = rootPath + File.separatorChar + "src" + File.separatorChar + "main" + File.separatorChar + "resources" + File.separatorChar + fileName;
        getFileContent(filePath);
    }
 
    /**
     * 通过绝对路径获取项目中文件的位置(不能用于服务器)
     *
     * @param fileName 文件名
     * @throws IOException IO异常
     */
    public static void function7(String fileName) throws IOException {
        //参数为空
        File directory = new File("");
        //规范路径:getCanonicalPath() 方法返回绝对路径,会把 ..\ 、.\ 这样的符号解析掉
        String rootCanonicalPath = directory.getCanonicalPath();
        String filePath = rootCanonicalPath + File.separatorChar + "src" + File.separatorChar + "main" + File.separatorChar + "resources" + File.separatorChar + fileName;
        getFileContent(filePath);
    }
 
    /**
     * 根据文件路径读取文件内容
     *
     * @param fileInPath 文件路径
     */
    public static void getFileContent(Object fileInPath) throws IOException {
        BufferedReader br = null;
        if (fileInPath == null) {
            return;
        }
        if (fileInPath instanceof String) {
            br = new BufferedReader(new FileReader((String) fileInPath));
        } else if (fileInPath instanceof InputStream) {
            br = new BufferedReader(new InputStreamReader((InputStream) fileInPath));
        }
        String line;
        while (true) {
            assert br != null;
            if ((line = br.readLine()) == null) {
                break;
            }
            System.out.println(line);
        }
        br.close();
    }
}
posted @   Java小白的搬砖路  阅读(197)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)

喜欢请打赏

扫描二维码打赏

支付宝打赏

点击右上角即可分享
微信分享提示