当前位置:网站首页>文件拷贝的实现

文件拷贝的实现

2020-11-08 21:03:52 8Years

public class TestIO01 {


    public static void main(String[] args) {


        copy("D:/aaa.txt","D:/B.txt");//把前者文件的内容拷贝到后者文件中qu

    }

    
    public static void copy(String srcfile,String destpath)  {
        File file=new File(srcfile);
        File file1=new File(destpath);
        InputStream is=null;
        OutputStream os=null;

        try {
            is=new FileInputStream(file);
            os=new FileOutputStream(file1);
            byte[] car=new byte[3];
            int temp;
            int len;


            while((len=is.read(car))!=-1){
                os.write(car,0, len);
                String str=new String(car,0,len);
            }
            os.flush();

        } catch (FileNotFoundException fileNotFoundException) {
            fileNotFoundException.printStackTrace();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }finally{

            try{
                if(is!=null){
                    is.close();
                }
            }catch (IOException ioException){
                ioException.printStackTrace();
            }



            try{
                if(is!=null){
                    is.close();
                }
            }catch (IOException ioException){
                ioException.printStackTrace();
            }


        }


    }
}

版权声明
本文为[8Years]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4583813/blog/4707700