IO流读取文件,通过IO流复制文件

203 阅读2分钟

如果仅仅为解决读取文件的需求,可以导入commons-io包,可以极大简化读写文件操作,并且还有很多方便的功能,这里就不展开

使用InputStream/Reader读文件

inputStream:字节流

Reader: 字符流

其余的输入流都是这个流的子类,字节流可以处理一些字符流不能处理的文件,如图片,音频等,字符流处理字符类文件txt 速度更快

public static void readByInputStream(){

  InputStream inputStream = null;
  byte[] bytes = new byte[1024];
  StringBuilder sb = new StringBuilder();
  URL path = IO_Read.class.getClassLoader().getResource("Result_5.csv");
  try{
    inputStream = new FileInputStream(path.getPath());
    int len;
    while((len = inputStream.read(bytes)) != -1){
      sb.append(new String(bytes,0,len));
    }
  }catch (Exception e){
    e.printStackTrace();
  }finally{
    try{
      inputStream.close();
    } catch (NullPointerException | IOException e) {
      e.printStackTrace();
    }
  }
  System.out.println(sb.toString());
}

这里用了byte数组来缓存数据,当然我们可以使用BufferInputStream 缓冲流来加快读写的速度 同样的,缓冲流有

BufferedInputStream 和 BufferedOutputStream

BufferedReader 和 BufferedWriter


public static List<String> readTxt(){
    InputStream inputStream = null;
    BufferedReader bufferedReader = null;
    String fileName = "100330-goodsInfo-2021-01-2122222.xlsx";
    StringBuilder sb = new StringBuilder();
    List<String> res = new ArrayList<>();
    try{
      inputStream = IO_Read.class.getClassLoader().getResourceAsStream(fileName);
      bufferedReader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("utf-8")));
      String line = "";

      while (null != (line = bufferedReader.readLine()) ){
        res.add(line);
        //此时 line 是txt文本的第一行数据
        sb.append(line+"\r\n");
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally{
      if(inputStream != null){
        try{
          inputStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      if(bufferedReader != null){
        try{
          bufferedReader.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    System.out.print(sb.toString());
    return res;
  }  

这里使用了转换流,将inputStream 转成成了 InputStreamReader ,在放入了 BufferedReader 缓存中

同样用BufferInputStream也是可以完成的

输出和输入是很相同的,只是输出中没有声明会覆盖掉原文件

一个复制方法

private static void Copy01(String srcPath, String targetPath) {
       File src = new File(srcPath);
       InputStream is = null;
       OutputStream os = null;

       try {
           is = new FileInputStream(src);
           os = new FileOutputStream(new File(targetPath));

           byte[] read = new byte[1024*10];
           int rlen;
           while ((rlen = is.read(read)) != -1) {
               os.write(read, 0, read.length);

           }
           os.flush();

       } catch (IOException e) {
           e.printStackTrace();
       } finally {

           try {
               if (is == null) {
                   is.close();
               }
           } catch (IOException e) {
               e.printStackTrace();
           }

           if (os == null) {
               try {
                   os.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }
   }