-->

Java: How to read directory folder, count and disp

2020-08-04 10:39发布

问题:

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;
}