IO_字节流_拷贝

文件的拷贝

——以程序为桥梁(文件的写入写出的综合应用)

2、操作

1)建立联系    File对象   源头—目的地

2)选择流   

                文件输入流 InputStream FileInputStream

                文件输出流 OutputStream FileOutputStream

3)操作    拷贝—不断的读取、不断的写出

                数组大小+read / write   

                byte[ ] source= new byte[1024] 

                int len = 0 ;

                while(-1!=(len=输入流.read( source))){

                         输出流.write( source,0,len )

                }

                输出流.flush

4)  释放资源  关闭   两个流

/**
 * 操作
 * 1)建立联系    File对象   源头—目的地
 * 2)选择流
 * 文件输入流 InputStream FileInputStream
 * 文件输出流 OutputStream FileOutputStream
 * 3)操作    拷贝—不断的读取、不断的写出
 * 数组大小+read / write
 * byte[ ] source= new byte[1024]
 * int len = 0 ;
 * while(-1!=(len=输入流.read( source))){
 * 输出流.write( source,0,len )
 * }
 * 输出流.flush
 * 4)  释放资源  关闭   两个流
 *
 * @author 
 * @date 2018/5/27 22:39
 */
public class CopyFile {
    public static void main(String[] args) {
        String srcPath = "F:/test/1.txt";
        String desPath = "F:/test/p/100.txt";
        try {
            copyFILE(srcPath,desPath);
        } catch (IOException e) {
            System.out.println("文件不存在,拷贝文件失败");
            e.printStackTrace();
        }
    }
    
    /**
     *  提取为方法
     * @param srcPath 源路径
     * @param desPath 目标路径
     * @throws IOException
     */
    public static void copyFILE(String srcPath,String desPath) throws IOException {
        //1、建立联系 源(文件存在)—目的地(文件可以不存在(视文件而定))
        File source = new File(srcPath);
        File destination = new File(desPath);

        if (!source.isFile()){//不是文件或者为null
            System.out.println("只能拷贝文件");
            throw new IOException("只能拷贝文件");
        }

        //2、选择流
        InputStream is = new FileInputStream(source);
        OutputStream os = new FileOutputStream(destination);
        //3、文件拷贝——循环读取和写出文件
        byte[] flu = new byte[1024];//缓存数组
        int len = 0;//定义的接收长度
        //读取
        while (-1!=(len = (is.read(flu)))){
            //写出
            os.write(flu,0,len);
        }
        os.flush();//强制刷出
        //关闭流——规则:先打开的后关闭
        os.close();
        is.close();
    }
}


public class CopyFileUtil {
    public static void copyFILE(String srcPath,String desPath) throws IOException {
        //1、建立联系 源(文件存在)—目的地(文件可以不存在(视文件而定))
        copyFILE(new File(srcPath),new File(desPath));
    }
    /**
     *  文件的拷贝
     * @param src 源文件File对象
     * @param des 目标文件FIle对象
     */
    public static void copyFILE(File src,File des) throws IOException {
        if (!src.isFile()){//不是文件或者为null
            System.out.println("只能拷贝文件");
            throw new IOException("只能拷贝文件");
        }

        //2、选择流
        InputStream is = new FileInputStream(src);
        OutputStream os = new FileOutputStream(des);
        //3、文件拷贝——循环读取和写出文件
        byte[] flu = new byte[1024];//缓存数组
        int len = 0;//定义的接收长度
        //读取
        while (-1!=(len = (is.read(flu)))){
            //写出
            os.write(flu,0,len);
        }
        os.flush();//强制刷出

        //关闭流——规则:先打开的后关闭
        os.close();
        is.close();
    }
}


文件夹的拷贝

1、递归查找子孙级文件、文件夹

2、文件  直接复制(IO流复制)

     文件夹  创建

/**
 * 文件夹的拷贝
 * 1、如果是文件则直接复制 copyFiles
 * 2、如果是文件夹则创建 mkdirs( )
 * 3、递归查找子孙级
 * @author chenpeng
 * @date 2018/5/27 23:50
 */
public class CopyDir {
    public static void main(String[] args) {
        String srcPath = "F:/test/p";
        String desPath = "F:/test/q";
        copyDir(srcPath,desPath);
    }

    /**
     * 拷贝文件夹
     * @param srcPath 源路径
     * @param desPath 目标路径
     */
    public static void copyDir(String srcPath,String desPath){
        File src = new File(srcPath);
        File des = new File(desPath);
        copyDir(src,des);
    }


    /**
     * 拷贝文件夹
     * @param src 源File对象
     * @param des 目标File对象
     */
    public static void copyDir(File src,File des){
        if (src.isDirectory()){//文件夹
            des = new File(des,src.getName());
        }
        copyDirDetail(src,des);
    }

    /**
     * 拷贝文件夹细节
     * @param src
     * @param des
     */
    public static void copyDirDetail(File src,File des){
        if (src.isFile()){//文件
            try {
                CopyFileUtil.copyFILE(src,des);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if (src.isDirectory()){//文件夹
            //确保目标文件夹存在
            des.mkdirs();
            //获取下一级的目录或者文件
            for (File sub:src.listFiles()) {
                copyDirDetail(sub,new File(des,sub.getName()));
            }
        }
    }
}
    


猜你喜欢

转载自blog.csdn.net/qq_30604989/article/details/80473510
今日推荐