java文件拷贝

单字节拷贝:

class CopyUtil {
    private CopyUtil() {}    //构造私有化
    //1.检测源文件目录是否存在
    public static boolean fileExists(String path) {
        return new File(path).exists();    //文件路径存在返回true
    }
    //2.如果目标目录不存在,创建父目录
    public static void createParentDirectory(String path) {
        File file = new File(path);
        if(!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
    }
    //3.创建IO流并调用拷贝方法
    public static boolean copy(String src,String des) throws IOException {
        boolean flag = true;    //状态提示
        File inFile = new File(src);    //源文件路径
        File outFile = new File(des);    //目标文件路径
        InputStream input = null;    //输入流
        OutputStream output = null;    //输出流
        try {
            input = new FileInputStream(inFile);    //输入流对象
            output = new FileOutputStream(des);    //输出流对象
            copyHandle(input,output);    //文件拷贝处理
        } catch(Exception e) {
            flag = false;    //出现异常转换状态和finally一起抛出
        } finally {
            input.close();    //无论程序是否正常执行,最后关闭流
            output.close();
        }
        return flag;
    }
    //4.文件拷贝处理
    private static void copyHandle(InputStream input,OutputStream output) throws IOException {
        int temp;    //单个字节数据    
        while((temp = input.read()) != -1) {//读取源文件信息直到数据末尾
            output.write(temp);//写入目标文件信息
        }
    }
}
public class Copy {
    public static void main(String[] args) {
        //如果不是源文件目录和目标目录,系统结束
        if(args.length != 2) {    
            System.out.println("错误执行方式");
            System.exit(1);    
        }
        if(CopyUtil.fileExists(args[0])) {    //检测源文件是否存在
            CopyUtil.createParentDirectory(args[1]);
            try {
                System.out.println(CopyUtil.copy(args[0],args[1]) ? "拷贝成功" : "拷贝失败");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            System.out.println("源文件不存在");
        }
    }
}

多字节拷贝

class CopyUtil {
    private CopyUtil() {}    //构造私有化
    //1.检测源文件目录是否存在
    public static boolean fileExists(String path) {
        return new File(path).exists();    //文件路径存在返回true
    }
    //2.如果源文件目录不存在,创建父目录
    public static void createParentDirectory(String path) {
        File file = new File(path);
        if(!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
    }
    //3.创建IO流并调用拷贝方法
    public static boolean copy(String src,String des) throws IOException {
        boolean flag = true;    //状态提示
        File inFile = new File(src);    //源文件路径
        File outFile = new File(des);    //目标文件路径
        InputStream input = null;    //输入流
        OutputStream output = null;    //输出流
        try {
            input = new FileInputStream(inFile);    //输入流对象
            output = new FileOutputStream(des);    //输出流对象
            copyHandle(input,output);    //文件拷贝处理
        } catch(Exception e) {
            flag = false;    //出现异常转换状态和finally一起抛出
        } finally {
            input.close();    //无论程序是否正常执行,最后关闭流
            output.close();
        }
        return flag;
    }
    //4.文件拷贝处理
    private static void copyHandle(InputStream input,OutputStream output) throws IOException {
        int temp;    //多个字节数据
        byte[] bt = new byte[1024];
        input.read(bt);    //添加1024个数组元素
        while((temp = input.read(bt)) != -1) {    //存在数组元素时执行写入工作
            output.write(bt,0,temp);    //读取当前数组所有元素
        }
    }
}
public class Copy {
    public static void main(String[] args) {
        //如果不是源文件目录和目标目录,系统结束
        if(args.length != 2) {    
            System.out.println("错误执行方式");
            System.exit(1);    
        }
        if(CopyUtil.fileExists(args[0])) {    //检测源文件是否存在
            CopyUtil.createParentDirectory(args[1]);
            try {
                System.out.println(CopyUtil.copy(args[0],args[1]) ? "拷贝成功" : "拷贝失败");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            System.out.println("源文件不存在");
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值