Package Gao.org;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.File;public class ReadFile {public ReadFile () {}/** * reads all files under a directory */public static Boolean R Eadfile (String filepath) throws FileNotFoundException, IOException {try {file file = new File (filepath); if (!file.isdirectory ()) {System.out.println ("file"); System.out.println ("path=" + File.getpath ()); System.out.println ("absolutepath=" + File.getabsolutepath ()); System.out.println ("name=" + file.getname ()); } else if (File.isdirectory ()) {System.out.println ("directory"); string[] filelist = File.list (); for (int i = 0; i < filelist.length; i++) { File ReadFile = new file (filepath + "\ \" + filelist[i]); if (!readfile.isdirectory ()) {System.out.println ("path=" + readfile.get Path ()); System.out.println ("absolutepath=" + Readfile.getabsolutepa th ()); System.out.println ("name=" + readfile.getname ()); } else if (Readfile.isdirectory ()) {ReadFile (filepath + "\ \" + Filelist[i] ); }}}} catch (FileNotFoundException e) { System.out.println ("ReadFile () Exception:" + e.getmessage ()); } return true; } /** * Delete all directories and files under a directory *//*public static Boolean DeleteFile (String delpath) Throws FileNotFoundException, IOException {try {File file = new file (Delpath); if (!file.isdirectory ()) {System.out.println ("1"); File.delete (); } else if (File.isdirectory ()) {System.out.println ("2"); string[] filelist = File.list (); for (int i = 0; i < filelist.length; i++) {File Delfile = new File (Delpath + "\ \ "+ filelist[i]); if (!delfile.isdirectory ()) {System.out.println ("path=" + delfile.getpath ( )); System.out.println ("Absolutepath=" + Delfile.getabsolutepath ()); System.out.println ("name=" + delfile.getname ()); Delfile.delete (); System.out.println ("Delete file succeeded"); } else if (Delfile.isdirectory ()) {DeleteFile (delpath + "\ \" + Filelist[i] ); }} file.delete (); }} catch (FileNotFoundException e) {System.out.println ("DeleteFile () Exception:" + e.getmessage ()); } return true; }*/public static void Main (string[] args) {try {//can change the following path ReadFile ("d:\\users\\gao\\desktop\\bionlp2013data\\develop "); DeleteFile ("D:/file"); } catch (FileNotFoundException ex) {} catch (IOException ex) {} System.out. println ("OK"); }}
Java reads the files in the fixed directory (almost the same as the previous article)