Java 复制文件代码_在java中关于实现文件和文件的复制,提供示例代码

一、文件复制,示例方法代码如下:/**@b@* 复制单个文件@b@* @param srcPath String 原文件路径,如:c:/test.txt@b@* @param targetPath String 复制后路径@b@* @return 空@b@*/@b@public void copyFile(String srcPath,String targetPath){@b@        try{@b@            int ub=0;@b@            File srcfile=new File(srcPath);@b@            if(srcfile.exists()){//判断文件是否存在@b@                InputStream instr=new FileInputStream(srcPath);//读取文件@b@                FileOutputStream fs=new FileOutputStream(targetPath);//写入目标文件@b@                byte[] buf =new byte[1024];@b@                while ((ub = instr.read(buf)) != -1) {@b@                    fs.write(buf,0,ub);@b@                }    @b@                instr.close();    @b@            }@b@            @b@        }catch(Exception e){@b@            e.printStackTrace();@b@        }@b@    }

二、文件夹复制,示例方法代码如下:/**@b@* 复制整个文件夹内容@b@* @param srcPath String 原文件路径,如:c:/srcPath@b@* @param targetPath String 复制后路径@b@* @return 空@b@*/@b@public void copyFolder(String srcPath, String targetPath) {@b@        try {@b@            (new File(targetPath)).mkdirs();// 如果目标文件夹中如有不存在,首先创建出来@b@            File f = new File(srcPath);@b@            String[] fs = f.list();@b@            File tmp = null;@b@            for (int i = 0; i 

总结

java关于File类提供了文件创建、删除和移动,但没有提供复制方法,所以需要自己实现文件和文件夹复制功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值