Recursively iterate through all the files in a folder under a disk, and copy files to generate files and files with folders

Source: Internet
Author: User
Tags mkdir
Package com.hudong.test;
Import Java.io.File;
Import java.io.IOException;
Import java.util.ArrayList;
Import java.util.List;

Import Org.apache.commons.io.FileUtils; public class Ergodicfile {public static void main (string[] args) throws IOException {File File = new file ("
        E:\\ershouok1 ");
        Ergodicfile (file, 0);

    Ergodicfilefolder (file);
    /** * Generate Files * * @param file * @param temp * @return * @throws IOException * * public static list<file> ergodicfile (file file, int temp) throws IOException {list<file> List = new
        Arraylist<file> ();

        file[] filelist = File.listfiles (); for (int i = 0; i < filelist.length i++) {file Docfile = new File ("e:\\39yiyuan\\doc\\" + temp +. xml)
            ;
            File Summaryfile = new file ("e:\\39yiyuan\\summary\\" + temp +. xml);

        File ContentFile = new file ("e:\\39yiyuan\\content\\" + temp +. xml);    if (Filelist[i].isfile ()) {//judgment is file if ("Doc.xml". Equals (Filelist[i].getname ()) {   Fileutils.copyfile (Filelist[i], docfile); Copy file} else if ("Summary.xml". Equals (Filelist[i].getname ())) {Fileutils.copyfile (
                Filelist[i], summaryfile); else if ("Content.xml". Equals (Filelist[i].getname ()) {Fileutils.copyfile (filelist[i), contentfile)
                ;
            } else if (Filelist[i].isdirectory ()) {//Judgment is Directory Ergodicfile (Filelist[i], i);//recursive
    } return list; /** * Generate files with folders * * @param file * @param temp * @return * @throws IOException *  /public static list<file> ergodicfilefolder (file file) throws IOException {list<file> List = new
        Arraylist<file> ();

        file[] filelist = File.listfiles (); for (int i = 0; i < filelist.length; i++) {//Traverse file if (Filelist[i].isfile ()) {//judgment is file if ("Doc.xml". Equals (Filelist[i].getna
                    Me ()) {File Docfile = new file ("e:/yiyuan/doc/" + System.currenttimemillis ());
                    Docfile.mkdir ();
                Fileutils.copyfile (Filelist[i], new File (Docfile.getabsolutepath () + "/doc.xml")); else if ("Summary.xml". Equals (Filelist[i].getname ()) {File ContentFile = new file ("E:/yiyuan/summa
                    ry/"+ system.currenttimemillis ());
                    Contentfile.mkdir ();
                Fileutils.copyfile (Filelist[i], new File (Contentfile.getabsolutepath () + "/summary.xml")); else if ("Content.xml". Equals (Filelist[i].getname ()) {File Summaryfile = new file ("E:/yiyuan/conte
                    nt/"+ system.currenttimemillis ());
                    Summaryfile.mkdir (); Fileutils.copyfile (Filelist[i], new File (Summaryfile.getabsolutepath () + "/COntent.xml "));
            } else if (Filelist[i].isdirectory ()) {//Judgment is Directory Ergodicfilefolder (Filelist[i]);//recursive
    } return list;

 }
}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.