A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© cuilitang 中级黑马   /  2013-10-24 00:21  /  1379 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 cuilitang 于 2013-10-24 00:31 编辑
  1. package cn.itcast;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.util.Scanner;


  7. public class CopyDemo {
  8.         public static void main(String[] args){
  9.                 //String src = "G:\\temp";                        将这两个字符串改由Scanner接收
  10.                 //String dest = "F:";
  11.                 Scanner sc = new Scanner(System.in);
  12.                
  13.                 System.out.println("请输入需要被拷贝的文件夹的路径(路径中斜杠请使用双斜杠):");
  14.                 String src = sc.next();
  15.                 System.out.println("请输入拷贝目标位置:");
  16.                 String dest = sc.next();
  17.                 File srcfile = new  File(src);
  18.                 File destfile = new  File(dest);
  19.                 try{
  20.                         copy(srcfile,destfile);
  21.                 }catch(Exception e){
  22.                         throw new RuntimeException("拷贝文件失败,请检查源文件或者目标位置是否存在");
  23.                 }
  24.                 System.out.println("拷贝完毕,请查看。");
  25.                         
  26.                         
  27.         }
  28.                
  29.                 public static void copy(File srcfile,File destfile) throws Exception{
  30.                         //在目标位置创建同名文件夹
  31.                         File newDir = new File(destfile,srcfile.getName());
  32.                         newDir.mkdir();
  33.                         //遍历源文件夹
  34.                         File[] subFiles = srcfile.listFiles();
  35.                         for (File subfile : subFiles) {        
  36.                                  // 输出文件名,测试文件是否遍历成功
  37.                                 String name = subfile.getName();
  38.                                 System.out.println(name);
  39.                                 //如果是文件,直接拷贝
  40.                                 if(subfile.isFile()){
  41.                                         FileInputStream fileInputStream = new FileInputStream(subfile);
  42.                                         FileOutputStream fileOutputStream = new FileOutputStream(new File(newDir,subfile.getName()));
  43.                                        
  44.                                         int i = 0;
  45.                                         byte [] arr = new byte[1024];
  46.                                         while((i = fileInputStream.read(arr)) != -1){
  47.                                                 fileOutputStream.write(arr,0,i);
  48.                                                 fileOutputStream.flush();
  49.                                         }
  50.                                         fileInputStream.close();
  51.                                         fileOutputStream.close();
  52.                                         //如果不是文件夹,递归调用拷贝方法
  53.                                 }else{
  54.                                         copy(subfile,newDir);                        
  55.                                 }
  56.         
  57.                         }
  58.                         
  59.                 }
  60.         }
复制代码

2 个回复

倒序浏览
http://pan.baidu.com/s/1xeD9b
回复 使用道具 举报
IO流的由浅入深,很常见,不仅能解决关于文件的各种操作,也能对文件图片进行颜色的涂鸦等,这也是面试和笔试中问到最多的一个知识点之一!
加油!知识重在积累!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马