zoukankan      html  css  js  c++  java
  • 将一个文件夹中我们需要的文件拷贝到另一个文件夹中的代码实现

     1 package cn.itsource.homework;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileOutputStream;
     6 import java.util.ArrayList;
     7 /**
     8  * 需求:编写程序将D盘XML文件夹中所有以“avi”结尾的文件考入到E盘(通过代码建文件夹保存文件)
     9  * 
    10  * 
    11  * */
    12 public class CopyHM3 {
    13 
    14     public static void main(String[] args) throws Exception {
    15         //选择要复制的文件
    16          File file = new File("d:/XML");
    17          //调用下面的方法,得到我们所想要的所有文件
    18          getFiles(file);
    19          //新建一个文件放我们要复制的类容
    20          File file2 = new File("e:/java学习");
    21          file2.mkdirs();
    22          //遍历装有文件的数组
    23          for (File f : list) {
    24              //得到每个文件的名字
    25              String name = f.getName();
    26              //在新文件夹中创建相同名字的文件
    27              File file3 = new File(file2, name);
    28               file3.createNewFile();
    29               //字节输入流 从磁盘到内存
    30               FileInputStream inputStream = new FileInputStream(f);
    31               //字节输出流 从内存到磁盘
    32               FileOutputStream outputStream = new FileOutputStream(file3);
    33               byte[] b =new byte[1024];
    34               int num;
    35               while((num=inputStream.read(b))!=-1){
    36                   outputStream.write(b);
    37               }
    38               //关流,先开后关
    39               outputStream.close();
    40               inputStream.close();    
    41         }
    42          
    43     }
    44     //新建一个数组 将文件放入
    45      static  ArrayList<File> list =new ArrayList<File>();
    46     public static void getFiles(File f){
    47         //判断文件夹是否为空
    48         if(f!=null){
    49             //不为空判断是否是文件
    50              if(f.isFile()){
    51                  //获得文件名字,判断文件结尾
    52                  if( f.getName().endsWith(".avi")){
    53                      //将符合条件的文件放如数组中
    54                      list.add(f); 
    55                  }    
    56             //传入的是文件夹的情况
    57              }else {
    58                  //获取文件夹中所有文件,放入数组中
    59                  File[] files = f.listFiles();
    60                  //判断是否为空文件夹
    61                  if(files.length!=0){
    62                      //遍历数组
    63                      for (File file : files) {
    64                          //递归,继续执行方法
    65                         getFiles(file);
    66                     }
    67                      
    68                  }
    69             }
    70         }
    71         
    72     }
    73 }
  • 相关阅读:
    微信公众平台回复音乐
    Else is very important
    Generate source code from wsdl
    PHP Simple HTML DOM Parser: check elements with multiple classes
    Get Version is the first function is called?
    Remote debug GWT UI
    Remote Debug For Java Application On Tomcat
    SetStyleName引起的Regression Issue
    做乘法运算的时候需要考虑越界问题
    MySQL Uniall All
  • 原文地址:https://www.cnblogs.com/logoman/p/11343058.html
Copyright © 2011-2022 走看看