当前位置:网站首页>Realization of file copy

Realization of file copy

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

public class TestIO01 {


    public static void main(String[] args) {


        copy("D:/aaa.txt","D:/B.txt");// Copy the contents of the former file into the latter 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]所创,转载请带上原文链接,感谢