zoukankan      html  css  js  c++  java
  • java 将一个非空文件夹拷贝到另一个地方

    没有处理异常,只是简单的抛出了。需要捕获的需修改一下。

    public class Test001 {

    //把一个文件夹或文件移到另一个地方去。
    public static void main(String[] args) throws Exception {
    File file=new File("D:\testFolder");
    new Test001().copyFileTo(file, "D:\hasaki");
    }
    //filename,des目的地
    public void copyFileTo(File file,String des) throws Exception {
    String newPath=des+File.separator+file.getName();
    if(file.isDirectory()) {
    new File(newPath).mkdir();
    File[] files=file.listFiles();
    for(File f:files) {
    copyFileTo(f,newPath);
    }
    }else {
    copyFile(file,new File(newPath));
    }
    }
    public void copyFile(File src,File copy) throws Exception {
    InputStream fis=new FileInputStream(src);
    OutputStream fos=new FileOutputStream(copy);
    byte[] bytes=new byte[1024];
    int len=0;
    while((len=fis.read(bytes))!=-1) {
    fos.write(bytes,0,len);
    }
    fis.close();
    fos.close();
    }
    }

  • 相关阅读:
    Photoshop 基础七 位图 矢量图 栅格化
    Photoshop 基础六 图层
    Warfare And Logistics UVALive
    Walk Through the Forest UVA
    Airport Express UVA
    Guess UVALive
    Play on Words UVA
    The Necklace UVA
    Food Delivery ZOJ
    Brackets Sequence POJ
  • 原文地址:https://www.cnblogs.com/yuezeyuan/p/7866200.html
Copyright © 2011-2022 走看看