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+"字节");
        
    }
}

 

相关文章:

  • 2021-06-22
  • 2021-09-24
  • 2022-02-02
  • 2021-09-21
  • 2021-10-21
  • 2021-09-10
  • 2021-12-07
  • 2021-08-25
猜你喜欢
  • 2022-12-23
  • 2021-05-28
  • 2021-06-23
  • 2021-09-29
  • 2022-12-23
  • 2021-04-16
  • 2022-12-23
相关资源
相似解决方案