首页 > 代码库 > 用IO字节流复制文件-CopyFileByIo

用IO字节流复制文件-CopyFileByIo

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

//文件复制 
//E:/3.jpg ---> D:/3copy.jpg
public class CopyFileByIo {
    
    public static void main(String[] args) {
        
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            //输入流读取 E:/3.jpg
             fis=new FileInputStream("e:/3.jpg");
            //输出流
             fos=new FileOutputStream("d:/3copy.jpg");
            //字节缓冲区
            byte[] buffer=new byte[1024];
            int len=0;
            while((len=fis.read(buffer))!=-1){
                fos.write(buffer, 0, len);
                fos.flush();
            }
            System.out.println("复制成功!");
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

用IO字节流复制文件-CopyFileByIo