IT虾米网

Java如何实现文件拷贝

dflying 2023年01月30日 编程语言 27 0
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(); 
            } 
 
 
        } 
 
 
    } 
}

本文参考链接:https://www.yisu.com/zixun/541292.html
评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

Java装饰器怎么写