Java: How to read directory folder, count and display no of files and copy to another folder?

佐手、 提交于 2019-12-13 12:23:00

问题


I have to read a folder, count the number of files in the folder (can be of any type), display the number of files and then copy all the files to another folder (specified).

How would I proceed?


回答1:


i Have to read a folder, count the number of files in the folder (can be of any type) display the number of files

You can find all of this functionality in the javadocs for java.io.File

and then copy all the files to another folder (specified)

This is a bit more tricky. Read: Java Tutorial > Reading, Writing and Creating of Files (note that the mechanisms described there are only available in Java 7 or later. If Java 7 is not an option, refer to one of many previous similar questions, e.g. this one: Fastest way to write to file? )




回答2:


you have all the sample code here :

http://www.exampledepot.com

http://www.exampledepot.com/egs/java.io/GetFiles.html

File dir = new File("directoryName");

String[] children = dir.list();
if (children == null) {
    // Either dir does not exist or is not a directory
} else {
    for (int i=0; i<children.length; i++) {
        // Get filename of file or directory
        String filename = children[i];
    }
}

// It is also possible to filter the list of returned files.
// This example does not return any files that start with `.'.
FilenameFilter filter = new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return !name.startsWith(".");
    }
};
children = dir.list(filter);


// The list of files can also be retrieved as File objects
File[] files = dir.listFiles();

// This filter only returns directories
FileFilter fileFilter = new FileFilter() {
    public boolean accept(File file) {
        return file.isDirectory();
    }
};
files = dir.listFiles(fileFilter);

The copying http://www.exampledepot.com/egs/java.io/CopyDir.html :

// Copies all files under srcDir to dstDir.
// If dstDir does not exist, it will be created.
public void copyDirectory(File srcDir, File dstDir) throws IOException {
    if (srcDir.isDirectory()) {
        if (!dstDir.exists()) {
            dstDir.mkdir();
        }

        String[] children = srcDir.list();
        for (int i=0; i<children.length; i++) {
            copyDirectory(new File(srcDir, children[i]),
                                 new File(dstDir, children[i]));
        }
    } else {
        // This method is implemented in Copying a File
        copyFile(srcDir, dstDir);
    }
}

However is very easy to gooole for this stuff :)




回答3:


I know this is too late but below code worked for me. It basically iterates through each file in directory, if found file is a directory then it makes recursive call. It only gives files count in a directory.

public static int noOfFilesInDirectory(File directory) {
    int noOfFiles = 0;
    for (File file : directory.listFiles()) {
        if (file.isFile()) {
            noOfFiles++;
        }
        if (file.isDirectory()) {
            noOfFiles += noOfFilesInDirectory(file);
        }
    }
    return noOfFiles;
}


来源:https://stackoverflow.com/questions/13045326/java-how-to-read-directory-folder-count-and-display-no-of-files-and-copy-to-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!