文件的拷贝以及封装成工具类

本章内容:

拷贝:以程序作为中转,把输入流和输出流合二为一
 *   工具类:1.封装操作和释放资源步骤,简化代码

文件的拷贝

 /*
    文件的拷贝
    1.文件到程序
    2.程序到文件
     */
    public static void fileCopy(String srcPath,String detaPath){
        File src = new File(srcPath);
        File deta = new File(detaPath);
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(src);
            os = new FileOutputStream(deta);
            //分段读取,实际开发中,一般是几K的读取,1024就是1K,10K=1024*10这样写
            byte[] flush = new byte[6];//缓冲容器
            int len = -1;//接收长度
            while ((len = is.read(flush))!=-1){
                os.write(flush,0,len);
            }
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {//先开的后关闭
            try {
                if(null!=os){
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(null!=is){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

文件和文件夹都可以拷贝

 public static void dirCopy(File file, File toFile)  {
        byte[] b = new byte[1024];
        int a;
        FileInputStream fis =null;
        FileOutputStream fos = null;
        String filepath = file.getAbsolutePath();//获取绝对路径
        filepath=filepath.replaceAll("\\\\", "/");//对路径进行处理
        String toFilepath = toFile.getAbsolutePath();
       toFilepath=toFilepath.replaceAll("\\\\", "/");
        int lastIndexOf = filepath.lastIndexOf("/");//符号 /  最后一次出现的位置
        toFilepath = toFilepath + filepath.substring(lastIndexOf ,filepath.length());//到这一步都是在为新文件夹写路径
        File copy=new File(toFilepath);
        if (file.isDirectory()) {
            if (!copy.exists()) {
                copy.mkdir();
            }
            //遍历文件夹
            for (File f : file.listFiles()) {
                dirCopy(f, copy);
            }
        } else {
            if (toFile.isDirectory()) {
                toFile = new File(toFilepath);
            }
            try {
                fis = new FileInputStream(file);
                fos = new FileOutputStream(toFile);
                while ((a = fis.read(b)) != -1) {
                    fos.write(b, 0, a);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(null!=fos){
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if(null!=fis){
                        fis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

工具类:jdk1.7对资源释放做了处理:try...with...resource:(因为我的是1.8的,没做测试)

 public static void fileCopyToPack1(InputStream is ,OutputStream os){
//        try (is;os ){//不需要finally,只要添加这个 就会自动释放了,也不需要关心前后顺序
//            //分段读取,实际开发中,一般是几K的读取,1024就是1K,10K=1024*10这样写
//            byte[] flush = new byte[1024];//缓冲容器
//            int len = -1;//接收长度
//            while ((len = is.read(flush))!=-1){
//                os.write(flush,0,len);
//            }
//            os.flush();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

自己封装:先对释放资源步骤进行封装

 public static void close(InputStream is ,OutputStream os){
        try {
            if(null!=os){
                os.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if(null!=is){
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

释放资源步骤再简化

 /*
    释放资源再简化,参数是可变参数,类似于数组,可以一个也可以多个,增加了灵活性。
    因为InputStream和OutputStream都实现于Closeable接口
     */
    public static void close(Closeable... ios){
        for(Closeable io:ios){
            try {
                if(null!=io){
                    io.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
InputStream和OutputStream都实现于Closeable接口
public abstract class InputStream implements Closeable 
public abstract class OutputStream implements Closeable, Flushable

封装操作步骤和释放步骤

 //封装操作和释放步骤,只要传入输入输出流就行了。
    public static void fileCopyToPack(InputStream is ,OutputStream os){
        try {
            //分段读取,实际开发中,一般是几K的读取,1024就是1K,10K=1024*10这样写
            byte[] flush = new byte[1024];//缓冲容器
            int len = -1;//接收长度
            while ((len = is.read(flush))!=-1){
                os.write(flush,0,len);
            }
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {//先开的后关闭
           close(is,os);
        }
    }

调用:

 //文件到文件
        try {
            InputStream is = new FileInputStream("fileTest.txt");
            OutputStream os = new FileOutputStream("testFile.txt");
            fileCopyToPack(is,os);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //文件到字节数组
        byte[] datas =null;
        try {
            InputStream is = new FileInputStream("fileTest.txt");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            fileCopyToPack(is,baos);
            datas = baos.toByteArray();
            System.out.println(datas.length);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //字节数组到文件
        try {
            InputStream is = new ByteArrayInputStream(datas);
            OutputStream os = new FileOutputStream("testFile.txt");
            fileCopyToPack(is,os);
        } catch (IOException e) {
            e.printStackTrace();
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值