Java réalise les fichiers de lecture et de suppression du dossier
Package Test.com; (String FilePath) lève FileNotFoundException, ioException {file file = new File (filepath); path = "+ file.getPath ()); // System.out.println (" Absolutepath = "+ file.getAbsolutepath ()); tname ());} else if (file.isdirectory ()) {String [] FileList = file.list (); ); ()); System.out.println (readFile.getName ());} else if (readFile.isDirectory ()) {readFile (FilePath + "/" + fis Lelist [i];}}} Catch (FileToTFoundException E) {System.out.println ("readFile () Exception:" + e.getMessage ());} return true;} / *** Supprimer un dossier Tous les dossiers ci-dessous et fichier * / / * public static boyan DeleteFile (String Delpath ) Lève FileNotFoundException, ioException {file file = new File (DelPath); ()) {System.out.println ("2"); String [] FileList = File.List (); // "+ FileList [i]); if (! delfile.isdirectory ()) {System.out.println (" path = "+ deldpath ("); System.out.println ("Absolutepath =" + delfile.getabsolupath ()); ) {DeleteFile (Delpath + "//" + FileList [i]);}} file.delete (); ());} Return true;} * / public static void main (String [] arts) {try {c: // users / /// Desktop // skj_h25 positif // 004_rcag // 003_skj "); / / DeleteFile ("d: / file");} catch (filenotfoundegn ex) {} catch (ioException ex) {} tige.out.println (ok ");}}
Méthode deux:
Package autrement Le dossier * * / public class TestReadfile {/ ** * Le chemin absolu du webroot du projet * @return * / string "// d: / $ {workspace} / $ {projectName} / webroot / web-inf / classes / "的路径 String path = this.getClass (). getResource (" / "). getPath (); // 从路径字符串 retirer le chemin d'ingénierie Path = path.substring (1, path.indexof (" web -inf / class "); System.out.println (" Chemin d'ingénierie: "+ chemin); chemin de retour;} / *** @ @ @ @ @ param args * / public static void main (String [] args) {TestReAmFile Trf = new TestReamFile (); String XmlPath = trf.getProjectPath () + "TestOcs"; .PrintStackTrace ();} / ** * Tous les fichiers sous le dossier de chemin spécifié * @param filepath * @return * @throws filenotffffff oundexception * @throws ioexception * / public static boolean readallfile (string filepath) lance filenotfoundException, ioexception {fichier file file = Nouveau fichier (filepath); ()); System.out.println ("/ t Absolute Path =" File.getAbsolupath ()); (file.isDirectory ()) {System.out.println ("/ n Filder Fichier Liste des informations:"); ++) {file readful = fileList [i]; N / n / n / n / n / n / n / n / n / n / n / n / n / t relative path = "+ readFile.getPath ()); System.out.println (" / t Absolute Path = " + readFile.getabsolutepath ()); ));}}} {System.out.pr intln ("readFile () Exception:" + e.getMessage ());} return true;} / ** * supprimer tous les dossiers et fichiers sous un dossier * @param delpath * @Return * @throws filenotfoundeption * @throws ioException * / public static boolean DeleteFile (String Delpath) lève FileNotFoundException, ioException {try {file file = new File (Delpath); .p Rintln ("1"); 0; )); isDirectory ()) {DeleteFile (FileList [i] .getPath ());}} file.delete ();}}} (filenotFoundException e) {System.out.println ("DeleteFile ()" e.getMessage (); } Retour True;}}
Ce qui précède est tout le contenu de cet article.