Write all files recursively traverse a directory in Java

java get file attributes such as file size and modification time:

mysize = file.length Long ();
Long file.lastModified the lastModified = ();
System.out.println ( "Size:");
the SimpleDateFormat the dateFormat the SimpleDateFormat new new = ( "the MM-dd-YYYY HH: mm: SS"); // note case is not the same, the general return date and time in milliseconds and are basic units of bytes

DateFormat.format // (the lastModified);
System.out.println ( "Size:" + mysize + "," + " Modified:" + dateFormat.format (the
lastModified));
} ---------- ------
Disclaimer: this article is CSDN blogger original article "Bryant pen", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_20172379/article/details/87804061

This link: https: //blog.csdn.net/qq_34309305/article/details/79194343
First of all, get this title, I'm looking for how to achieve the java to read the file.

File file = new File (fileName) ;
we know java get file directory by the above code
File [] files = file.listFiles () ;
The above code retrieves all sub-folders and files in the directory.
The following is a statement java.io.File.isFile () method:

public boolean isFile ()
parameters

NA

return value

If and only if this abstract pathname indicates that the file is a document the method returns true, otherwise the method returns false.

By isFile () function can be determined whether a file.
By the isDirectory () function can be determined whether the directory.
Knowing this will be easier.

java.io.File Import;

public class Counter2 {

public static void main (String [] args) {
// get the target directory
File File = new new File ( "D:");
// get directory erupted files and subfolders
File [] = File.listFiles Files ();
ReadFile (Files);

}

public static void ReadFile (File [] Files) {
IF (Files == null) {// if the directory is empty, exit
return;
}
for (File F: files) {
// if the file name directly to the output
IF (f.isFile ()) {
System.out.println (f.getName ());
}
// if the folder, the recursive call
else if (f .isDirectory ()) {
ReadFile (f.listFiles ());
}
}
}
}

Guess you like

Origin www.cnblogs.com/eryun/p/11607725.html