IO流的学习--文件夹下文件的复制

package com.nick.IO;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 练习:复制文件夹下的所有内容到另一个文件夹中(代码创建目标文件夹)
 * e:\\io中的内容复制到d:\\io(代码创建)下
 * 
 * 基本思路
 * 1、动态生成目的文件夹的名字
 * 2、动态生成文件夹中的内容
 * 3、别把各种变量名弄混
 * @author nickY
 *
 */
public class CopyTest2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        copyDir(new File("e:\\io"), new File("d:\\"));
    }
    
    public static void copyDir(File src, File dest) {
        String srcFolderName = src.getName(); //得到io
        
        File destFolderName = new File(dest, srcFolderName);
        //System.out.println(destFileName.getPath());
        destFolderName.mkdirs(); //在d盘下创建了名为io的文件夹
        
        //遍历源文件夹中的文件
        File[] srcFilesNames = src.listFiles();
        
        for (File srcFileName : srcFilesNames) {
            //System.out.println(file); 带路径的文件
            String fileName = srcFileName.getName(); //文件名
            File destFilesNames = new File(destFolderName, fileName); //在目标文件夹下创建File对象
            //进行复制
            copyFile(srcFileName, destFilesNames);
        }
        
    }

    private static void copyFile(File srcFileName, File destFilesNames) {
        // TODO Auto-generated method stub
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            FileInputStream fileInputStream = new FileInputStream(srcFileName);
            FileOutputStream fileOutputStream = new FileOutputStream(destFilesNames);
            bufferedInputStream = new BufferedInputStream(fileInputStream);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            
            byte[] b = new byte[1024];
            int len = 0;
            while((len = bufferedInputStream.read(b)) != -1) {
                bufferedOutputStream.write(b, 0, len);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                if(bufferedOutputStream != null) {
                    bufferedOutputStream.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if(bufferedInputStream != null) {
                    bufferedInputStream.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        
    }

}

posted on 2018-10-09 08:28  thelast9527  阅读(271)  评论(0编辑  收藏  举报

导航