zoukankan      html  css  js  c++  java
  • 简单IO流应用之文件目录拷贝

    文件目录的拷贝

    在学习IO流通常遇到目录的操作,涉及到一层层的目录和子孙级文件,往往使用递归思想。

    递归思想之巧妙,但要处理大量的函数压栈和出栈问题,效率并不高。

    主要思路:

    将问题模块化:文件目录的拷贝其实就是分为

    模块1:从源操作目录拷贝文件到目的操作目录

    模块2:在目的操作文件目录下创建新的目录

    模块1实现:

     1 public static void test(String srcPath,String destPath) {
     2     
     3         /**方法1、
     4          * BufferedReader  封装成缓存流
     5          * InputStreamReader 字符流输入
     6          * FileInputStream 字节流输入
     7          * 就相当于 把字符内容(文件)-->字节流(程序可以识别)-->转换成字符流丢进缓存区
     8          */
     9             try(BufferedReader reader = new BufferedReader(
    10                     new InputStreamReader(new FileInputStream(srcPath)));
    11                     BufferedWriter writer = 
    12                             new BufferedWriter(
    13                             new OutputStreamWriter(
    14                                     new FileOutputStream(destPath)))){
    15                     String line = null;
    16                     while((line = reader.readLine())!=null) {
    17 
    18                         writer.write(line);
    19                         writer.flush();                    
    20                     }
    21                 }catch(IOException e) {
    22                 e.printStackTrace();
    23             }
    24     }
    25     
    26     public static void test2(String srcPath,String destPath) {
    27         
    28         /**方法2、
    29          * BufferedReader  封装成缓存流
    30          * FileReader 字符流读取输入,相当于把字符(文件内容)扔进去了
    31          * FileInputStream 字节流输入
    32          * 就相当于 把字符内容a(文件)-->字节流(程序可以识别)-->转换成字符流丢进缓存区
    33          */
    34             try(BufferedReader reader = new BufferedReader(
    35                     new FileReader(srcPath));
    36                     BufferedWriter writer = new BufferedWriter(
    37                             new FileWriter(destPath))){
    38                     String line = null;
    39                     while((line = reader.readLine())!=null) {
    40 
    41                         writer.write(line);
    42                         writer.flush();                    
    43                     }
    44                 }catch(IOException e) {
    45                 e.printStackTrace();
    46             }
    47     }

    模块2实现:

     1     public static void Copy(String srcPath,String destPath) {
     2         
     3         File src = new File(srcPath); //创建源操作目录文件
     4         File[] files = src.listFiles(); //列出子文件(里面包括目录)
     5         for(int i = 0; i<files.length; i++) {
     6             if(files[i].isDirectory()) {  //如果是目录
     7                 File destNext = new File(destPath+File.separator+files[i].getName());
     8                 destNext.mkdirs(); //在目标文件夹中创建目录
     9                 //递归
    10                 Copy(srcPath+File.separator+files[i].getName(),destPath+File.separator+files[i].getName());
    11             }else {//如果是文件,那就拷贝
    12                 test2(files[i].getAbsolutePath(),destPath+File.separator+files[i].getName());
    13             }
    14         }    
    15     }

    全部代码:

     1 package io应用之文件夹拷贝;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.BufferedWriter;
     5 import java.io.File;
     6 import java.io.FileInputStream;
     7 import java.io.FileOutputStream;
     8 import java.io.FileReader;
     9 import java.io.FileWriter;
    10 import java.io.IOException;
    11 import java.io.InputStreamReader;
    12 import java.io.OutputStream;
    13 import java.io.OutputStreamWriter;
    14 
    15 /**
    16  * 文件夹的拷贝,包括里面的子文件和子文件夹
    17  * 思路:
    18  * 1、拿到一个文件对象(目录),在目的路径下创建一个目录
    19  * 2、列出所有的文件对象,有遇到文件copy到新的目录,如果遇到目录
    20  * 则进入目的的同级的文件目录下再创建目录,依次递归...
    21  * 3、递归出口是没有发现目录和文件
    22  * @author liuzeyu12a
    23  *
    24  */
    25 public class CopyDir {
    26 
    27     public static void main(String[] args) {
    28 
    29         String srcPath="C:/Users/liuzeyu12a/Desktop/洛谷OJ";
    30         String destPath = "E:/JAVA/workspac/测试"; 
    31                 
    32             int index =  srcPath.lastIndexOf('/');
    33             String subSrcPath = srcPath.substring(index, srcPath.length()-1);
    34             destPath = destPath+subSrcPath; //此时已经为创建"洛谷"OJ文件夹做准备了
    35         Copy(srcPath,destPath);
    36     }
    37 
    38     public static void test(String srcPath,String destPath) {
    39         /**方法1、
    40          * BufferedReader  封装成缓存流
    41          * InputStreamReader 字符流输入
    42          * FileInputStream 字节流输入
    43          * 就相当于 把字符内容(文件)-->字节流(程序可以识别)-->转换成字符流丢进缓存区
    44          */
    45             try(BufferedReader reader = new BufferedReader(
    46                     new InputStreamReader(new FileInputStream(srcPath)));
    47                     BufferedWriter writer = 
    48                             new BufferedWriter(
    49                             new OutputStreamWriter(
    50                                     new FileOutputStream(destPath)))){
    51                     String line = null;
    52                     while((line = reader.readLine())!=null) {
    53 
    54                         writer.write(line);
    55                         writer.flush();                    
    56                     }
    57                 }catch(IOException e) {
    58                 e.printStackTrace();
    59             }
    60     }
    61     
    62     public static void test2(String srcPath,String destPath) {        
    63         /**方法2、
    64          * BufferedReader  封装成缓存流
    65          * FileReader 字符流读取输入,相当于把字符(文件内容)扔进去了
    66          * FileInputStream 字节流输入
    67          * 就相当于 把字符内容a(文件)-->字节流(程序可以识别)-->转换成字符流丢进缓存区
    68          */
    69             try(BufferedReader reader = new BufferedReader(
    70                     new FileReader(srcPath));
    71                     BufferedWriter writer = new BufferedWriter(
    72                             new FileWriter(destPath))){
    73                     String line = null;
    74                     while((line = reader.readLine())!=null) {
    75 
    76                         writer.write(line);
    77                         writer.flush();                    
    78                     }
    79                 }catch(IOException e) {
    80                 e.printStackTrace();
    81             }
    82     }
    83 
    84     public static void Copy(String srcPath,String destPath) {        
    85         File src = new File(srcPath); //创建源操作目录文件
    86         File[] files = src.listFiles(); //列出子文件(里面包括目录)
    87         for(int i = 0; i<files.length; i++) {
    88             if(files[i].isDirectory()) {  //如果是目录
    89                 File destNext = new File(destPath+File.separator+files[i].getName());
    90                 destNext.mkdirs(); //在目标文件夹中创建目录
    91                 //递归
    92                 Copy(srcPath+File.separator+files[i].getName(),destPath+File.separator+files[i].getName());
    93             }else {//如果是文件,那就拷贝
    94                 test2(files[i].getAbsolutePath(),destPath+File.separator+files[i].getName());
    95             }
    96         }    
    97     }
    98 }
    View Code

    参考资料:

    https://www.cnblogs.com/rainmer/p/4605064.html

     1 package io深入学习;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.BufferedWriter;
     5 import java.io.File;
     6 import java.io.FileInputStream;
     7 import java.io.FileOutputStream;
     8 import java.io.FileReader;
     9 import java.io.FileWriter;
    10 import java.io.IOException;
    11 import java.io.InputStream;
    12 import java.io.OutputStream;
    13 
    14 /**
    15  * 比较当我们使用缓冲流和没有使用缓存流之间的对比
    16  * @author liuzeyu12a
    17  *
    18  */
    19 public class BufferedCharIoCopy {
    20 
    21     public static void main(String[] args) {
    22             long time1 = System.currentTimeMillis();
    23             copy2("char.txt","cde.txt");        
    24             long time2 = System.currentTimeMillis();
    25             System.out.println(time2-time1);
    26     }
    27     public static void copy(String srcPath,String destPath) {
    28         //1、创建源
    29             File src = new File(srcPath); //源头
    30             File dest = new File(destPath);//目的地
    31             //2、选择流
    32             InputStream  is =null;
    33             OutputStream os =null;
    34             try {
    35                 is =new FileInputStream(src);
    36                 os = new FileOutputStream(dest);        
    37                 //3、操作 (分段读取)
    38                 byte[] flush = new byte[1024]; //缓冲容器
    39                 int len = -1; //接收长度
    40                 while((len=is.read(flush))!=-1) {
    41                     os.write(flush,0,len); //分段写出
    42                 }            
    43                 os.flush();
    44             }catch (IOException e) {
    45                 e.printStackTrace();
    46             }finally{
    47                 //4、释放资源 分别关闭 先打开的后关闭
    48                 try {
    49                     if (null != os) {
    50                         os.close();
    51                     } 
    52                 } catch (IOException e) {
    53                     e.printStackTrace();
    54                 }
    55                 
    56                 try {
    57                     if(null!=is) {
    58                         is.close();
    59                     }
    60                 } catch (IOException e) {
    61                     e.printStackTrace();
    62                 }
    63             }
    64     }
    65     
    66     //套上字符缓存流
    67     public static void copy2(String srcPath,String destPath) {
    68         try(BufferedReader is =  new BufferedReader(new FileReader(srcPath));
    69             BufferedWriter os = new BufferedWriter( new FileWriter(destPath))) {
    70             try {
    71                 String line =null;
    72                 while((line=is.readLine())!=null) {
    73                     os.write(line);
    74                 }
    75             } catch (IOException e) {
    76                 e.printStackTrace();
    77             }
    78         } catch (IOException e1) {        
    79             e1.printStackTrace();
    80         } 
    81     }
    82 }
    View Code
     1 package 测试io;
     2 
     3 import java.io.File;
     4 
     5 public class FileDirDemo01 {
     6 
     7     // 文件夹长度
     8     private int len;
     9     // 文件夹路径
    10     private String path;
    11     // 文件夹个数
    12     private int dirSize;
    13     // 文件个数
    14     private int fileSize;
    15 
    16     // 文件源
    17     private File src;
    18 
    19     // 构造方法,创建file对象
    20 
    21     public FileDirDemo01(String path) {
    22         this.path = path;
    23         this.src = new File(path);
    24         dirCount(this.src);
    25     }
    26 
    27     public static void main(String[] args) {
    28         FileDirDemo01 dirDemo = new FileDirDemo01("E:\JAVA\workspac\BF算法与KMP算法");
    29         System.out.println("文件夹大小" + dirDemo.len + "文件个数" + dirDemo.fileSize + "文件夹个数" + dirDemo.dirSize);
    30     }
    31 
    32     //文件计数,文件夹
    33     public void dirCount(File f) {
    34 
    35         if (f.exists() && f != null) {
    36 
    37             if (f.isFile()) {
    38                 len += f.length();
    39                 this.fileSize++; // 文件个数
    40             } else {
    41                 this.dirSize++;
    42                 for (File flies : f.listFiles()) {
    43                     dirCount(flies);
    44                 }
    45             }
    46         }
    47     }
    48 
    49 }
    View Code
  • 相关阅读:
    Java lamda Stream
    java动态绑定的一点注意
    javascript的一点学习
    阶段总结
    some notes about spring aop
    java 命令notes
    Guava cache
    位运算
    解析JDK 7的动态类型语言支持
    Maven里面多环境下的属性过滤(配置)
  • 原文地址:https://www.cnblogs.com/liuzeyu12a/p/10355653.html
Copyright © 2011-2022 走看看