经典指数          
原因
734
浏览数
0
收藏数
 

编写代码删除某文件目录下的所有子目录和文件,并输出目录的名字,路径以及文件的内容。

     举报   纠错  
 
切换
1 个答案
本题目解体思路:(1)查询当前路径下文件夹和文件(2)递归调用文件夹下的子文件夹和文件(3)读出文件里面的内容(4)删除文件接着删除文件夹 /**** * 查询指定目录下的文件和文件夹 *  * @param args * @throws IOException */ public static void queryFile(String strPath) throws IOException { File file = new File(strPath); File[] tempList = file.listFiles(); System.out.println("该目录下对象个数:" + tempList.length); for (int i = 0; i < tempList.length; i++) { if (tempList[i].isFile()) { readFile(tempList[i]); System.out.println("文     件:" + tempList[i]); } if (tempList[i].isDirectory()) { // 如果是文件夹 queryFile(tempList[i].getPath()); System.out.println("文件夹:" + tempList[i]); } tempList[i].delete(); } } public static void readFile(File f) throws IOException { FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); String content = "";// 用来显示文件内容 while ((content = br.readLine()) != null) { System.out.println(content); } }
 
切换
撰写答案
扫描后移动端查看本题