zoukankan      html  css  js  c++  java
  • java 提取目录下所有子目录的文件到指定位置

    package folder;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;

    public class folder {
    /**
    * java 提取目录下所有子目录的文件到指定位置
    * @throws IOException
    *
    * */
    public static void main(String[] args) throws IOException {
    String uploadFilePath="D:\AFile";
    Map<String,String> fileNameMap=new HashMap<String, String>();
    display(fileNameMap);
    listfile(new File(uploadFilePath),fileNameMap);


    }

    private static void display(Map<String, String> fileNameMap) throws IOException {
    // TODO Auto-generated method stub
    Iterator it2=fileNameMap.keySet().iterator();
    while (it2.hasNext()){
    Object key=it2.next();
    String value=fileNameMap.get(key);
    File f=new File(key.toString());
    if(!f.isDirectory()){
    String s="D:\BFile"+value;
    copyFile(key.toString(),s);
    }
    }



    }

    private static void copyFile(String src, String dest) throws IOException {
    // TODO Auto-generated method stub
    FileInputStream in=new FileInputStream(src);
    File file=new File(dest);
    if(!file.exists()){
    file.createNewFile();
    FileOutputStream out=new FileOutputStream(file);
    int c;
    byte buffer[]=new byte[1024];
    while((c=in.read(buffer))!=-1)
    {
    for (int i = 0; i < c; i++) {
    out.write(buffer[i]);
    }
    }

    }

    }

    private static void listfile(File file, Map<String, String> fileNameMap) {
    // TODO Auto-generated method stub
    //如果file代表的不是一个文件,而是一个目录
    if(!file.isFile()){
    //列出该目录下的所有文件和目录
    File files[]=file.listFiles();
    for (File f : files) {
    listfile(f,fileNameMap);
    }
    }
    else{
    String realName=file.getName();
    fileNameMap.put(file.toString(), realName);
    }
    }

    }

    java 搞这个 要写这么多代码。

    Python 搞这些就方便很多。  一句话就可以了,比较方便。

  • 相关阅读:
    PHP:第一章——PHP中的goto语句和
    PHP:第二章——PHP中的foreach语句
    Swingr的JTextField、JPasswordField设置圆角输入框
    Jtabbedpane设置透明、Jpanel设置透明
    去掉utf-8的Bom头:使用java以及jdbc不使用第三方库执行sql文件脚本
    使用java以及jdbc不使用第三方库执行sql文件脚本
    JButton ButtonClickTest
    Md5加密
    JButton变换样式
    grub2详解(翻译和整理官方手册)
  • 原文地址:https://www.cnblogs.com/szw-blog/p/5478131.html
Copyright © 2011-2022 走看看