Spring项目读取resource下的文件

目录

  1、前提条件java

  2、使用ClassPathResource类读取web

    2.一、Controller、service中使用ClassPathResourcespring

    2.二、单元测试使用ClassPathResourceapp

  3、使用FileSystemResource类读取文件单元测试

  

 

1、前提条件

  要去读取的文件是存放在project/src/main/resources目录下的,以下图中的test.txt文件。测试

  

 

 

2、使用ClassPathResource类读取

2.一、Controller、service中使用ClassPathResource

  无论是在哪一层(service、controller..),均可以使用这种方式,甚至是单元测试中,也是能够的。ui

package cn.ganlixin.demo.controller;

import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

@RestController
public class TestController {

    @RequestMapping("testFile")
    public String testFile() throws IOException {
        // ClassPathResource类的构造方法接收路径名称,自动去classpath路径下找文件
        ClassPathResource classPathResource = new ClassPathResource("test.txt");
        
        // 得到File对象,固然也能够获取输入流对象
        File file = classPathResource.getFile();
        
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        StringBuilder content = new StringBuilder();
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            content.append(line);
        }
        
        return content.toString();
    }
}

 

2.二、单元测试使用ClassPathResource

  单元测试也是可使用ClassPathResource,可是须要注意:spa

  一、单元测试的资源目录默认是project/src/test/resources,若是该目录下找到单元测试须要的文件,那么就用找到的文件;操作系统

  二、若是在单元测试的资源目录下没有找到单元测试须要的文件,就会去找/project/src/main/resources目录下的同名文件进行操做。code

package cn.ganlixin.demo.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationConfigTest {

    @Test
    public void testFile() throws IOException {
        final ClassPathResource classPathResource = new ClassPathResource("test.txt");
        final File file = classPathResource.getFile();

        final BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
    }
}

  

3、使用FileSystemResource类读取文件

  FileSystemResource这个类在找文件的时候就是按照给定的路径名去找,默认的当前目录就是项目根目录

  使用该类来查找文件时,须要保证文件路径彻底正确,另外,在代码中将路径写死是一个很差的习惯,特别是一个文件的路径在不一样的主机上的位置(层级目录)不必定相同,因此咱们开发过程当中不多使用这种方式。

  FileSystemResource的用法和ClassPathResource的用法类似,由于他们都继承了AbstractResource这个抽象类。

package cn.ganlixin.demo.example;

import org.junit.Test;
import org.springframework.core.io.FileSystemResource;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileTest extends ApplicationConfigTest {

    @Test
    public void testFile() throws IOException {

        FileSystemResource resource = new FileSystemResource("./");
        System.out.println(resource.getFile().getAbsolutePath());
        // 传入当前路径,得到的是项目根目录:/Users/ganlixin/code/Spring/demo/example/.

        // 传入根目录路径,得到的就是操做系统的根目录
        resource = new FileSystemResource("/");
        System.out.println(resource.getFile().getAbsolutePath());  // 输出 /

        // 获取单元测试resources目录下的test.txt,须要指定详细的路径
        resource = new FileSystemResource("src/test/resources/test.txt");
        final File file = resource.getFile();

        final BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
    }
}

  

 

  这里就列举了两种方式,还有其余不少方式,这两种应该够用了

相关文章
相关标签/搜索