java复制指定文件

package com.cn.file;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopy {


    @Test
    public void copyFile(){

        String file1Path="F:\\beijing.jpg";
        String file2Path="F:\\y\\1.jpg";

        FileOutputStream fileOutputStream=null; //输出流用于写入
        FileInputStream fileInputStream=null;  //输入流用于读取
        byte[] buff=new byte[1024];
        int size=0;
        try {
            fileOutputStream=new FileOutputStream(file2Path);
            fileInputStream=new FileInputStream(file1Path);

            while((size=fileInputStream.read(buff))!=-1){
                fileOutputStream.write(buff,0,size);
                //边读边写
            }

            System.out.println("Copy ok");

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileOutputStream.close();
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}



posted @ 2023-02-03 20:04  赤叶秋枫  阅读(24)  评论(0编辑  收藏  举报