Java File 和 各种流

1.Java读取File文件

1.1读取相对路径下的file

方式一:Spring 的 ResourceLoader:

    @Autowired
    private ResourceLoader resourceLoader;

  //path 为resources 文件
public void initWorkFlow(String path) throws Exception { String line = null; { Resource workFlowResource = resourceLoader.getResource("classpath:"+path); BufferedReader brWorkFlow = new BufferedReader(new InputStreamReader(workFlowResource.getInputStream())); StringBuilder sb = new StringBuilder(); while ((line = brWorkFlow.readLine()) != null) { sb.append(line); } String result = sb.toString();   }

方式二:用Java的Files和Paths:

    public void getPDDocument(String path) throws IOException {
        byte[] in = Files.readAllBytes(Paths.get(new File("").getAbsolutePath()+"/src/test/resources/"+path));
        String result = new String(in);
    }

1.2

1.3

2.

3.

4.

原文地址:https://www.cnblogs.com/kplsm123/p/9103682.html