Java復制文件工具類

gxw6 9年前發布 | 2K 次閱讀 Java

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

public class FileUtils {

public FileUtils() {  
    // TODO Auto-generated constructor stub  
}  
/** 
 * @warning The name of file must be end with .xls 
 * @param res The resource file 
 * @param des The destination 
 * @return  
 * @throws FileNotFoundException  
 */  
public static boolean toCopy(String res,String des){  
    boolean flag=false;  
    Boolean bool1 = res.endsWith(".xxx");  
    Boolean bool2 = des.endsWith(".xxxb");  

    if(!bool1 && !bool2){  
        return false;  
    }  
    //輸入源文件  
    File file = new File(res) ;  
    FileInputStream fr=null;  
    //復制目標文件  
    File desFile = new File(des);  
    FileOutputStream bw=null;  
    try {  
        fr = new FileInputStream(file);  
        bw = new FileOutputStream(desFile);  

        //buffer  
        byte[] b = new byte[512];  
        while(fr.read(b)!=-1){  
            bw.write(b);  
        }  
        bw.flush();  
        flag=true;  
    } catch (FileNotFoundException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }catch (IOException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }finally{  
        if(fr != null)  
            try {  
                fr.close();  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        if(bw != null){  
            try {  
                bw.close();  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }  
    }  
    return flag;  
}  

} </pre>
把.xxx格式的文件復制到指定路徑。

 本文由用戶 gxw6 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!