The Java tool provides us with a class for managing the file system, which is the file class, which differs from other stream classes in that the stream class is concerned with the contents of the files. The file class is concerned with the storage of files on disk.
One, the file class has multiple constructors, and the constructors that are used frequently are:
1. Public File (String pathname) {}
Create a file object under the pathname path
2. Public File (String path,string name) {}
Creates a file object with the given name in the folder specified by Path parameter. Assuming that path is NULL, the constructor uses the current folder to create the object
3,public file (file dir, String name) {}
The file Object dir represents a folder that creates a file object with the given name in the folder specified by the dir parameter . assuming that dir is null,
The constructor will use the current folder to create the object
Two. To get permission properties for a file:
1, indicates whether the file is readable and writable. Can run
Boolean CanRead ()
Boolean canWrite ()
Boolean CanExecute ()
2. Sets the readable file . Writable, can be run
Boolean setreadable (Bollean state,bollean owneronly)
Boolean setwritable (bollean state,bollean owneronly)
Boolean setexecutable (bollean state,bollean owneronly)
3, deleting files
boolean delete ()
< Span style= "Font-family:simhei; Font-size:14px "> returns True if the file is deleted, otherwise false
void Deleteonexit ()
< Span style= "Font-family:simhei; font-size:14px "> Delete a file when the virtual machine shuts down
4. Infer whether a file exists
Boolean exists ()
5, get the file path name
String Getcanonicalpath ()
Returns a string that includes the canonical pathname of the file this method uses the appropriate folder delimiter and is able to obtain the uppercase and lowercase processing options chosen by the underlying file system
String GetName ()
Returns a string that includes the file name of this file object. But does not include path information
6. Infer whether file is a document or a folder
Boolean isdirectory ()
Infer if it is a folder
Boolean isfile ()
Infer whether to be a file
7. Get the file name and folder name included in the file object
String[] List ()
Returns the file name and folder name consisting of a character creation array
String[] List (filenamefilter filter)
Returns a string array that contains the file name and folder name for the filter condition included in this file object
File also has many method properties, with more ability to view API documentation
Now, use the file class to traverse all the files in a folder. My program process is:
1, get the file object for pathname
2, infer whether the file or folder exists. Output reminders on console when not present
3. The inference hypothesis is not a folder. It is inferred that the file is not a file, then the file path is output
4, get a string array of all file names and folder names under this folder
5. Assumed to be a folder. Search depth currentdepth+1, after the output folder name. for recursion
6, if it is a file, the direct output file name
Programs such as the following:
Import Java.io.file;import Java.io.ioexception;public class Dirergodic {private static int depth=1;public static void fin D (String pathname,int depth) throws Ioexception{int filecount=0;//get pathName file Object File Dirfile = new file (pathName);// Infer whether the file or folder exists. Does not exist when the console outputs a reminder if (!dirfile.exists ()) {System.out.println ("Do not Exit"); The inference hypothesis is not a folder, it is inferred is not a file, when the file is output file path if (!dirfile.isdirectory ()) {if (Dirfile.isfile ()) {System.out.println ( Dirfile.getcanonicalfile ());} return;} for (int j = 0; j < depth; J + +) {System.out.print ("");} System.out.print ("|--"); System.out.println (Dirfile.getname ());//Get all file names and folder names under this folder string[] fileList = Dirfile.list (); int currentdepth= depth+1;for (int i = 0; i < filelist.length; i++) {//traverse file Folder string = Filelist[i];//file ("DocumentName", "FileName ") is file and there is a constructor file File = new file (Dirfile.getpath (), string); String name = File.getname ();//assumed to be a folder. Search depth depth++, after the output folder name. Recursive if (File.isdirectory ()) {//Recursive find (File.getcanonicalpath (), currentdepth);} else{//If it is a file, the direct output file name for (int j = 0; J < Currentdepth; J + +) {System.out.print ("");} System.out.print ("|--"); SYSTEM.OUT.PRINTLN (name);}}} public static void Main (string[] args) throws Ioexception{find ("D:\\mongodb", Depth);}}
Test:
Java traverses all files in a folder