Spring boot 工程获取resources目录下文件的绝对路径

public static void main(String[] args)throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("1.txt");
        Resource resource = new ClassPathResource("1.txt");
        //获1.txt的取相对路径
        String classPathStr = classPathResource .getFile().getPath();
        System.out.println(classPathStr); 
        String path = resource.getFile().getPath();
        System.out.println(path); 
}

使用的jar分别是:

org.springframework.core.io.ClassPathResource,

org.springframework.core.io.Resource

借鉴于:springboot java读取/获取resources相对路径https://blog.csdn.net/weixin_42169734/article/details/119329377?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1-119329377-blog-124421876.pc_relevant_paycolumn_v3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1-119329377-blog-124421876.pc_relevant_paycolumn_v3&utm_relevant_index=1

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要让Spring Boot读取相对路径下的文件,我们可以使用Resource类提供的方法进行操作。 首先,我们需要在Spring Boot的配置文件(比如application.properties或application.yml)中配置文件路径。我们可以设置一个属性,比如"file.path",并指定文件的相对路径。例如,如果我们想读取相对于项目根目录下的一个名为"data.txt"的文件,我们可以在配置文件中添加以下内容: file.path=data.txt 接下来,在我们的代码中,我们可以使用ResourceLoader类来获取这个文件。 首先,我们需要在我们的类中注入一个ResourceLoader对象: @Autowired private ResourceLoader resourceLoader; 然后,我们可以使用resourceLoader的getResource方法来获取文件的URL: String filePath = "classpath:" + file.path; Resource resource = resourceLoader.getResource(filePath); URL url = resource.getURL(); 最后,我们可以使用url对象来读取文件的内容。例如,我们可以使用Files类来读取文本文件的内容: Path path = Paths.get(url.toURI()); List<String> lines = Files.readAllLines(path); 现在,我们就可以通过lines变量来访问文件的内容了。 总结起来,如果想要Spring Boot读取相对路径下的文件,我们只需要在配置文件中指定文件的相对路径,并使用ResourceLoader来获取文件的URL,在代码中读取文件的内容即可。 ### 回答2: 在Spring Boot中,我们可以使用`ResourceLoader`类来读取相对路径下的文件。 首先,我们需要注入`ResourceLoader`对象: ```java @Autowired private ResourceLoader resourceLoader; ``` 然后,可以使用`resourceLoader.getResource()`方法获取文件的`Resource`对象。我们可以使用`classpath:`前缀来指定相对于classpath的相对路径,或者使用`file:`前缀来指定相对于文件系统的路径。 ```java Resource resource = resourceLoader.getResource("classpath:example.txt"); ``` 接下来,我们可以通过`resource.getInputStream()`方法获取文件的输入流,从而读取文件的内容。 ```java try (InputStream inputStream = resource.getInputStream()) { // 读取文件内容 } ``` 另外,如果我们想指定文件绝对路径,可以使用`file:`前缀: ```java Resource resource = resourceLoader.getResource("file:/path/to/file/example.txt"); ``` 需要注意的是,尽管Spring Boot的classpath默认是`classpath:`,但是我们仍然可以使用相对于classpath的表达式,例如: ```java Resource resource = resourceLoader.getResource("classpath:/files/example.txt"); ``` 总之,使用`ResourceLoader`对象和相应的前缀,我们可以方便地读取Spring Boot应用程序中相对路径下的文件。 ### 回答3: 在Spring Boot中,可以使用ResourceLoader来读取相对路径下的文件。 首先需要注入一个ResourceLoader对象到你的代码中,你可以通过@Autowired或者构造函数等方式来实现注入。 例如,你可以在一个Service类中注入一个ResourceLoader对象: ```java @Service public class MyService { @Autowired private ResourceLoader resourceLoader; // ... } ``` 然后,你可以使用ResourceLoader的getResource方法来获取相对路径下的文件,该方法需要传入一个相对路径字符串作为参数。 例如,假设你有一个名为"data.txt"的文件在相对路径"/resources"下,你可以通过如下代码获取文件Resource对象: ```java Resource resource = resourceLoader.getResource("classpath:data.txt"); ``` 其中,"classpath:"是用来指示文件位于classpath下的前缀。如果文件不位于classpath下,你可以使用其他适当的前缀,例如"file:"来指示文件位于文件系统下的绝对路径。 接下来,你可以使用Resource对象提供的方法来读取该文件的内容,例如使用getInputStream方法来获取文件的输入流,然后使用IO流操作来读取文件内容。 例如,你可以通过如下代码来读取文件内容并输出: ```java try (InputStream inputStream = resource.getInputStream()) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { // 处理异常 } ``` 这样,你就可以使用Spring Boot来读取相对路径下的文件了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值