java获取某个文件夹下的所有文件、删除文件
2016-01-29 11:47
666 查看
</pre><pre name="code" class="javascript">public static void main(String[] args) throws IOException { readFile("E:\\testFilePath"); deleteFile("E:\\testFilePath"); } /** * 获取文件路径,包括子文件夹下文件路径 * @param filePath * @return */ public static boolean readFile(String filePath) { File file = new File(filePath); if (!file.isDirectory()) { System.out.println("文件地址:" + file.getAbsolutePath()); } else if (file.isDirectory()) { String[] fileList = file.list(); for (int i = 0; i < fileList.length; i++) { File readfilePath = new File(filePath + "\\" + fileList[i]); if (!readfilePath.isDirectory()) { System.out.println("文件地址:" + readfilePath.getAbsolutePath()); } else if (readfilePath.isDirectory()) { readFile(filePath + "\\" + fileList[i]); } } } return true; } /** * 删除文件夹下所有文件,包括子文件夹下文件 * @param delpath * @return */ public static boolean deleteFile(String delpath) { File filePath = new File(delpath); if (!filePath.isDirectory()) { filePath.delete(); } else if (filePath.isDirectory()) { String[] fileList = filePath.list(); for (int i = 0; i < fileList.length; i++) { File delfile = new File(delpath + "\\" + fileList[i]); if (!delfile.isDirectory()) { System.out.println("文件地址:"+delfile.getAbsolutePath()); delfile.delete(); System.out.println("删除文件成功"); } else if (delfile.isDirectory()) { deleteFile(delpath + "\\" + fileList[i]); } } filePath.delete(); } return true; }
相关文章推荐
- 基于Spring MVC的Web应用开发(10) - Validation
- 基于Spring MVC的Web应用开发(9) - Exceptions
- 基于Spring MVC的Web应用开发(8) - Convert
- 基于Spring MVC的Web应用开发(7) - Headers
- java&testng中Assert
- eclipse显示adb is down错误,无法真机调试
- 基于Spring MVC的Web应用开发(6) - Response
- Java写到.txt文件,如何实现换行
- 基于Spring MVC的Web应用开发(5) - Redirect
- JAVA使用Dom4j组装、解析XML
- 基于Spring MVC的Web应用开发(4) - FileUpload
- web.xml之<context-param>与<init-param>的区别与作用++ springMVC
- 基于Spring MVC的Web应用开发(3) - Resources
- java的IO
- 基于Spring MVC的Web应用开发(2) - Log
- The Java™ Tutorials — Generics :Wildcards and Subtyping 泛型和子类
- 第 3 章 DOM 操作
- 基于Spring MVC的Web应用开发(1) - HelloWorld
- 【Spring】手动获取spring容器对象时,报no qualifying bean of type is defined
- Java算法求最大最小值,倒序,冒泡排序,斐波纳契数列,日历一些经典算法