1 ImportJava.io.File;2 3 Public classReadFile {4 5 Public Static voidMain (string[] args) {6 7 //path is the absolute path to the specified directory8String Path = "/users/tonychan/workspaces/myeclipse ci/zhangjiajie/webroot/pics";9 getFile (path); Ten One } A - //The absolute path to the given directory, getting all the files in that directory (subdirectories of files can also be recursively obtained) - Public Static voidgetFile (String path) { the //file objects can be files or directories -File File =NewFile (path); -file[] Array =file.listfiles (); - + for(inti = 0; i < Array.Length; i++) { - if(Array[i].isfile ()) { + //Only take file name ASystem.out.println ("^^ ^^ ^" +array[i].getname ()); at //Take file path and name -System.out.println ("#####" +array[i]); - //Take file path and name -SYSTEM.OUT.PRINTLN ("* * * *" +Array[i].getpath ()); -}Else if(Array[i].isdirectory ()) { - GetFile (Array[i].getpath ()); in } - } to } + -}
1. Spaces in the path under Mac, not escaped in Java
String Path = "/users/tonychan/workspaces/myeclipse ci/zhangjiajie/webroot/pics";
2. File objects can be files or directories
3. The file object involves three methods:
Java reads all files in the specified directory (files in subdirectories can also be recursively obtained)