Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring Boot 读取 resources 目录下文件 #59

Open
techiall opened this issue Aug 29, 2019 · 2 comments
Open

Spring Boot 读取 resources 目录下文件 #59

techiall opened this issue Aug 29, 2019 · 2 comments
Labels

Comments

@techiall
Copy link
Owner

techiall commented Aug 29, 2019

回头看了一下之前写的这篇文章,感觉很多不足之处

最近又遇到一样的需求,趁次机会,重新编写该文章。

resources 文件夹内容如下,假如要读取 user.txt 里面的文本,并按行输出。

├── resources
│   ├── application.yml
│   ├── data
│   │   └── user.txt
│   └── exception.json

user.txt 内容为。

admin	admin
root	root

可以使用 org.springframework.core.io.ClassPathResource 并配合 FileCopyUtils,demo 如下:

import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


    public void run() throws IOException {
        byte[] bytes = read("data/user.txt");
        List<String> list = Arrays.stream(new String(bytes).split("\n"))
                .collect(Collectors.toList());
        list.forEach(System.out::println);
    }

    private static byte[] read(String path) throws IOException {
        ClassPathResource resource = new ClassPathResource(path);
        return FileCopyUtils.copyToByteArray(resource.getInputStream());
    }

输出结果为:

admin   admin
root    root

当然了你也可以读取 resource 里面的其他文件,如 application.yml,一样是可以读取成功的。

other

很多人在开发的时候可以正常读取 resource 文件夹里面的文件,但是打包发布到线上,就报错。

很大原因是使用了诸如 File / Files / Paths 之类的方法。

(毕竟 ClassPathResource 里面有 getURI() / getPath() / getFile(),然后再去读取,然后你会发现这么读取都是失败的。

@techiall

This comment has been minimized.

@techiall

This comment has been minimized.

@techiall techiall removed the Hexo label Oct 27, 2019
@techiall techiall changed the title spring boot 读取 resource 目录下文件 Spring Boot 读取 resource 目录下文件 Sep 28, 2020
@techiall techiall changed the title Spring Boot 读取 resource 目录下文件 Spring Boot 读取 resources 目录下文件 Sep 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant