JAVA----文件拷贝

/**
 * 拷贝的方法类
 * @author RUANWENJUN
 *
 */
class CopyUtil{
    //构造器私有化
    private CopyUtil() {}
    /**
     * 判断源文件路径是否存在
     * @param srcfile  源文件 
     * @return 如果存在则返回true
     */
    public static boolean srcIsExist(String srcfile) {
        File file = new File(srcfile);
        if(!file.exists()) {
            System.out.println("源路径不存在");
            return false;
        }
        if(!file.isFile()) {
            System.out.println("源路径不是文件");
            return false;
        }
        if(!file.canExecute()) {
            System.out.println("源文件不可操作");
             return false;
        }
        return true;
    }
    /**
     * 判断目标路径下是否存在文件,若不存在则新建一个
     * @param goalfile 粘贴文件的路径
     * @return true返回成功
     */
    public static boolean goalIsExist(String goalfile)throws IOException {
        File file = new File(goalfile);
        if(file.getParentFile()==null) {
            file.mkdirs();
        }
        file.createNewFile();
        return true;
    }
    /**
     * 拷贝文件的方法
     * @param srcfile  源文件路径
     * @param goalfile  目标文件路径
     */
    public static void copy(String srcfile,String goalfile) {
        File infile = new File(srcfile);      
        File outfile = new File(goalfile);
        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(infile);    //拷贝的输入流
            output = new FileOutputStream(outfile);  //拷贝的输出流
            byte data [] = new byte[1024];
            int tem =0;
            while((tem =input.read(data))!=-1) {
                output.write(data, 0, tem);
            }
            System.out.println("拷贝完成");

        } catch (IOException e) {
            System.out.println(e);
        }finally {
            try {
                input.close();
                output.close();
            }catch(IOException e) {
                System.out.println(e);

            }
        }
    }


}
public class Copy {
    public static void main(String[] args)throws IOException {
        if(args.length!=2) {
            System.out.println("参数不对,请输入源文件路径  目标文件路径");
        }else {
            if(CopyUtil.srcIsExist(args[0])) {       //如果源文件路径存在且可以拷贝
                CopyUtil.goalIsExist(args[1]);       //目标文件
                CopyUtil.copy(args[0], args[1]);
            }else {
                System.out.println("源文件有问题");
            }


        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值