java io流(字节流)

复制文件

//复制文件
//使用字节流
//复制文本文件用字符流,复制其它格式文件用字节流

import java.io.*;

public class Index{
    public static void main(String[] args) throws Exception{
        
        //字符流方式
        //FileReader fz = new FileReader("E:/1.txt");
        //FileWriter zt = new FileWriter("E:/2.txt");
        
        //字节流方式
        FileInputStream fz = new FileInputStream("D:/KuGou/刘欢 - 在路上.mp3");
        FileOutputStream zt = new FileOutputStream("D:/223.mp3");
        
        int aa;
        int fz_size = 0;    //统计文件有多大
        aa = fz.read();    //读取1字节
        while(aa!=(-1)){
            zt.write(aa); //写入1字节
            aa = fz.read();    //继续读取1字节
            fz_size++;
        }
        
        //将流中残留内容全部输出
        //拷贝文件时,在关闭文件之前写一下这个,以完整拷贝文件
        zt.flush();
        
        fz.close();
        zt.close();
        
        System.out.println("文件复制成功,共"+fz_size+"字节");
        
    }
}

 

相关文章:

  • 2022-01-09
  • 2021-12-09
  • 2021-11-30
  • 2019-11-02
  • 2021-11-20
  • 2020-10-27
  • 2021-12-28
  • 2018-11-02
猜你喜欢
  • 2019-09-23
  • 2021-04-16
  • 2021-03-18
  • 2018-12-05
  • 2021-03-25
  • 2019-08-29
  • 2018-10-07
  • 2021-10-11
相关资源
相似解决方案