zoukankan      html  css  js  c++  java
  • 复制文件夹

     1 package cn.it.demo;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileOutputStream;
     6 import java.io.IOException;
     7 
     8 public class Demo04 {
     9     public static void main(String[] args) {
    10         File file1=new File("e:\other");
    11         File file2=new File("e:\ther");
    12         copy(file1,file2);
    13     }
    14     private static void copy(File file1,File file2) {
    15         FileInputStream fis=null;
    16         FileOutputStream fos=null;
    17         try {
    18             if(file1.isDirectory()) {
    19                 file2.mkdir();
    20                 String[] filepaths = file1.list();
    21                 for(String f:filepaths) {
    22                     //获取源文件路径和新文件路径
    23                     copy(new File(file1.getAbsolutePath()+file1.separator+f),new File(file2.getAbsolutePath()+file2.separator+f));
    24                 }
    25             }else if(file1.isFile()) {
    26                 fis=new FileInputStream(file1);
    27                 fos=new FileOutputStream(file2);
    28                 int len=0;
    29                 byte[] buf=new byte[1024];
    30                 while((len=fis.read(buf))!=-1) {
    31                     fos.write(buf, 0, len);
    32                 }
    33             }
    34         } catch (IOException e) {
    35             e.printStackTrace();
    36         }finally {
    37             try {
    38                 if(fis!=null) {
    39                     fis.close();
    40                 }
    41             } catch (IOException e) {
    42                 e.printStackTrace();
    43             }
    44             try {
    45                 if(fos!=null) {
    46                     fos.close();
    47                 }
    48             } catch (IOException e) {
    49                 e.printStackTrace();
    50             }
    51         }
    52     }
    53 }
  • 相关阅读:
    全站导航
    常用模块
    模块的引用的路径的查找
    类的魔术方法
    包装和授权
    类内置的attr属性
    反射
    三大特性之多态
    三大特性之封装
    python应用:爬虫框架Scrapy系统学习第二篇——windows下安装scrapy
  • 原文地址:https://www.cnblogs.com/may12138/p/8422783.html
Copyright © 2011-2022 走看看