以下是实现的代码:
| package com.borland.samples.welcome; import java.io.FileNotFoundException; import java.io.IOException; import java.io.File; public class ReadFile { public ReadFile() {} /** * 删除某个文件夹下的所有文件夹和文件 * @param delpath String * @throws FileNotFoundException * @throws IOException * @return boolean */ 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("删除文件成功"); } else if (delfile.isDirectory()) { deletefile(delpath + "\\" + filelist[i]); } } file.delete(); } } catch (FileNotFoundException e) { System.out.println("deletefile() Exception:" + e.getMessage()); } return true; } /** * 删除某个文件夹下的所有文件夹和文件 * @param delpath String * @throws FileNotFoundException * @throws IOException * @return boolean */ public static boolean readfile(String filepath) throws FileNotFoundException, IOException { try { File file = new File(filepath); if (!file.isDirectory()) { System.out.println("文件"); 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("文件夹"); 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.getPath()); System.out.println("absolutepath=" + readfile.getAbsolutePath()); 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; } public static void main(String[] args) { try { readfile("D:/file"); //deletefile("D:/file"); } catch (FileNotFoundException ex) { } catch (IOException ex) { } System.out.println("ok"); } } |
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
相关专题
- Java环境安装配置 (5658篇文章)
- Java编程开发手册 (8309篇文章)
- 开发框架:深入了解 Struts Validator (3次浏览)
- Java中的通信机制及与C/C API的集成 (1次浏览)
- 用Hibernate实现领域对象的自定义字段 (1次浏览)
- 精通Hibernate之映射继承关系(一) (0次浏览)
- 精通Hibernate之映射继承关系(二) (0次浏览)
- 美国计算机教授语出惊人:Java对学生有害 (0次浏览)
- JDK 6 JRE 6 Update 4 (0次浏览)
- 三步教你改善Java代码质量 (0次浏览)
- Java语言入门 简述Java语言回收机制 (0次浏览)
- 2008年Java开发者最迫切的五个期望 (0次浏览)



