First, let's look at folder traversal.
| The code is as follows: |
Copy code |
|
// By LYLtim
Import java. io. File;
Public class TestFile {
Public static void main (String [] args ){
File f = new File ("D:/Temp/LYLtim_OI ");
System. out. println (f. getName ());
List (f, 0 );
}
Static void list (File f, int lv ){
File [] childs = f. listFiles ();
For (int I = 0; I <childs. length; I ++ ){
For (int j = 0; j <lv; j ++)
System. out. print ("success ");
If (I = childs. length-1)
System. out. println ("success" + childs [I]. getName ());
Else
System. out. println ("success" + childs [I]. getName ());
If (childs [I]. isDirectory ())
List (childs [I], lv + 1 );
}
}
} |
Check the total number of files in the Java statistics folder.
| The code is as follows: |
Copy code |
|
// By LYLtim
Import java. io. BufferedReader;
Import java. io. File;
Import java. io. FileReader;
Import java. io. IOException;
Public class TestIO {
Static BufferedReader BR = null;
Static long Count = 0;
Public static void main (String [] args) throws IOException {
Trvs (new File ("D:/Temp/LYLtim_OI "));
System. out. print (Count );
}
Static void Trvs (File f) throws IOException {
File [] childs = f. listFiles ();
For (int I = 0; I <childs. length; I ++ ){
If (childs [I]. isFile ()){
BR = new BufferedReader (new FileReader (childs [I]);
While (BR. readLine ()! = Null) Count + = 1;
}
Else Trvs (childs [I]);
}
}
}
|
Result: 8258