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.

posted on 2018-05-29 10:14  万水千山z  阅读(103)  评论(0编辑  收藏  举报

导航