- 文件流复制拷贝:
可以把输入流的文件内容拷贝到输出流文件中。
importcn.hutool.core.io.FileUtil; importcn.hutool.core.io.IoUtil; importjava.io.BufferedInputStream; importjava.io.BufferedOutputStream; publicclassTest { publicstaticvoidmain(String[] args) { BufferedInputStreamin=FileUtil.getInputStream("H:\\project\\hutool-study\\src\\main\\resources\\file1.txt"); BufferedOutputStreamout=FileUtil.getOutputStream("H:\\project\\hutool-study\\src\\main\\resources\\file2.txt"); longcopySize=IoUtil.copy(in, out); System.out.println("finish. copySize = "+copySize); } }
- 逐行读取文件内容
importcn.hutool.core.io.FileUtil; importcn.hutool.core.io.IoUtil; importjava.io.BufferedInputStream; importjava.nio.charset.Charset; importjava.util.ArrayList; importjava.util.List; publicclassTest { publicstaticvoidmain(String[] args) { BufferedInputStreamin=FileUtil.getInputStream("H:\\project\\hutool-study\\src\\main\\resources\\file1.txt"); List<String>content=newArrayList<>(); IoUtil.readLines(IoUtil.getReader(in, Charset.defaultCharset()), content); content.forEach(System.out::println); } }