Java复制文件的代码

jopen 9年前

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;        }        }