复习IO流-复制文件夹

1.迭代1:实现一个文件的复制

import java.io.*;

/**
 * 功能:实现文件夹的复制
 * 技能:IO流  递归  创建目录
 * 
 * 
 * 步骤分析
     * 1:创建一个空的目的文件夹  mkdir()
     * 2.复制源文件夹下的所有文件到目的文件夹  IO流+循环
     * 3.重复以上两步,知道没有文件和子文件夹 递归
 * 
 *    问题1:应该使用字节流还是字符流
 *        字节流  不仅有文本文件,也可以有二进制文件
 *    
 *
 *    问题2:如何提高复制文件夹的速度
 *        使用缓冲流  BufferedInputStream和BufferedOuputStream
 *        中转站可以使用字节数组  byte [] buf = new byte[1024]
 *
 *
 *    问题3:问题迭代
 *        1.实现一个文件的复制
 *        2.复制一个文件夹下的所有文件到目的文件夹
 *        3.复制一个文件夹下的所有文件和子文件夹到目的文件夹    
 *
 * @author Administrator
 *
 */

public class TestCopyDir {

    public static void main(String[] args) {        
        copyFile("e:/readme.txt","e:/readme2.txt");    

    }
    
    public static void copyFile(String sourceFile,String destFile){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try{
            //1.创建一个输入流和输出流
            File file1 = new File(sourceFile);
            InputStream fis = new FileInputStream(file1);
            bis = new BufferedInputStream(fis);
            
            File file2 = new File(destFile);
            OutputStream fos = new FileOutputStream(file2);
            bos = new BufferedOutputStream(fos);
            
            
            //2.使用输入流和输出流完成复制
            //2.1准备一个中转站:水桶
            byte [] buf = new byte[1024];
            
            //2.2借助循环和中转站完成复制操作
            int len = bis.read(buf); //读取文件的内容到buf数组,返回的真正读取的字节数。文件结束,返回-1
            while(len !=-1){
                //写数组的内容到目的文件
                //fos.write(buf);//写1024
                bos.write(buf, 0, len);
                //再读一次
                len = bis.read(buf);
            }
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            //3.关闭输入流和输出流
            try {
                if(bis != null){
                    bis.close();
                }                
            } catch (IOException e) {
                e.printStackTrace();
            }        
            try {
                if(bos != null){
                    bos.close();
                }                
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

2.迭代2:复制一个文件夹下的所有文件到目的文件夹
import java.io.*;
public class TestCopyDir2 {

    public static void main(String[] args) {        
        //copyFile("e:/readme.txt","e:/readme2.txt");
        copyDir("e:/406sxt","e:/406sxt2");    }
    
    public static void copyDir(String ssourceDir,String sdestDir){
        //创建一个空的目的文件夹
        File destDir = new File(sdestDir);
        if(!destDir.exists()){
            destDir.mkdir();
        }
        //得到源文件夹下的所有文件并复制到目的文件夹
        File sourceDir = new File(ssourceDir);
        File [] files = sourceDir.listFiles();//不仅有文件,还有目录
        for(File f :files){
            if(f.isFile()){
                //复制  copyFile("e:/406sxt/log.txt","e:/406sxt2/log.txt");
                // copyFile("e:/406sxt/readme.txt","e:/406sxt2/readme.txt");
                copyFile(ssourceDir+"/"+f.getName(),sdestDir+"/"+f.getName());
            }
        }
    }
    
    public static void copyFile(String sourceFile,String destFile){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try{
            //1.创建一个输入流和输出流
            File file1 = new File(sourceFile);
            InputStream fis = new FileInputStream(file1);
            bis = new BufferedInputStream(fis);
            
            File file2 = new File(destFile);
            OutputStream fos = new FileOutputStream(file2);
            bos = new BufferedOutputStream(fos);
            
            
            //2.使用输入流和输出流完成复制
            //2.1准备一个中转站:水桶
            byte [] buf = new byte[1024];
            
            //2.2借助循环和中转站完成复制操作
            int len = bis.read(buf); //读取文件的内容到buf数组,返回的真正读取的字节数。文件结束,返回-1
            while(len !=-1){
                //写数组的内容到目的文件
                //fos.write(buf);//写1024
                bos.write(buf, 0, len);
                //再读一次
                len = bis.read(buf);
            }
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            //3.关闭输入流和输出流
            try {
                if(bis != null){
                    bis.close();
                }                
            } catch (IOException e) {
                e.printStackTrace();
            }        
            try {
                if(bos != null){
                    bos.close();
                }                
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

3.迭代3:最终实现
import java.io.*;

public class TestCopyDir3 {

    public static void main(String[] args) {        
        //copyFile("e:/readme.txt","e:/readme2.txt");
        copyDir("e:/406sxt","e:/406sxt2");
    }
    
    public static void copyDir(String ssourceDir,String sdestDir){
        //创建一个空的目的文件夹
        File destDir = new File(sdestDir);
        if(!destDir.exists()){
            destDir.mkdir();
        }
        //得到源文件夹下的所有文件并复制到目的文件夹
        File sourceDir = new File(ssourceDir);
        File [] files = sourceDir.listFiles();//不仅有文件,还有目录
        for(File f :files){
            if(f.isFile()){
                //复制  copyFile("e:/406sxt/log.txt","e:/406sxt2/log.txt");
                // copyFile("e:/406sxt/readme.txt","e:/406sxt2/readme.txt");
                copyFile(ssourceDir+"/"+f.getName(),sdestDir+"/"+f.getName());
            }
            if(f.isDirectory()){
                //copyDir("e:/406sxt/20171215-IO流","e:/406sxt2/20171215-IO流");
                copyDir(ssourceDir+"/"+f.getName(),sdestDir+"/"+f.getName());
            }
        }
    }
    
    public static void copyFile(String sourceFile,String destFile){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try{
            //1.创建一个输入流和输出流
            File file1 = new File(sourceFile);
            InputStream fis = new FileInputStream(file1);
            bis = new BufferedInputStream(fis);
            
            File file2 = new File(destFile);
            OutputStream fos = new FileOutputStream(file2);
            bos = new BufferedOutputStream(fos);
            
            
            //2.使用输入流和输出流完成复制
            //2.1准备一个中转站:水桶
            byte [] buf = new byte[1024];
            
            //2.2借助循环和中转站完成复制操作
            int len = bis.read(buf); //读取文件的内容到buf数组,返回的真正读取的字节数。文件结束,返回-1
            while(len !=-1){
                //写数组的内容到目的文件
                //fos.write(buf);//写1024
                bos.write(buf, 0, len);
                //再读一次
                len = bis.read(buf);
            }
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            //3.关闭输入流和输出流
            try {
                if(bis != null){
                    bis.close();
                }                
            } catch (IOException e) {
                e.printStackTrace();
            }        
            try {
                if(bos != null){
                    bos.close();
                }                
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

猜你喜欢

转载自blog.csdn.net/qq_39209492/article/details/83591407