java拷贝文件

java拷贝文件

FileInputStream

FileOutputStream

package com.vfsd.core;

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

public class CopyFileAndRename {
    
    public static void copyFile(String oldFileName,String newFileName) throws IOException {
        File oldFile = new File(oldFileName);
        File newFile = new File(newFileName);
        
        FileInputStream fileInputStreamObj = new FileInputStream(oldFile);
        
        FileOutputStream fileOutputStreamObj = new FileOutputStream(newFile);
        
        byte[] bytes = new byte[1024];
        int len1 = -1;
        while((len1=fileInputStreamObj.read(bytes))!=-1) {
            fileOutputStreamObj.write(bytes, 0, len1);
            fileOutputStreamObj.flush();
        }
        
        fileOutputStreamObj.flush();
        fileOutputStreamObj.close();
        
        fileInputStreamObj.close();
    }

}

 

 

 

 

###########################

posted @ 2021-12-27 17:08  西北逍遥  阅读(42)  评论(0编辑  收藏  举报