现在的位置: 首页 > 综合 > 正文

获取一个路径下的所有文件及其子文件,子文件夹下的子文件….

2012年10月16日 ⁄ 综合 ⁄ 共 1136字 ⁄ 字号 评论关闭

public static void 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++) {
     System.out.println("filelist="+filelist);
     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());
  }
 }

 public static void main(String[] args) {
  try {
  readfile("E:/cmxie/data");
   //deletefile("D:/file");
  }
  catch (FileNotFoundException ex) {
  }
  catch (IOException ex) {
  }
 }

抱歉!评论已关闭.