我的空間,寫我所寫,禪我所藏

          與我一起遨游吧

           

          java讀取文件夾下的所有文件夾和文件

          package com.borland.samples.welcome;

          import java.io.FileNotFoundException;
          import java.io.IOException;
          import java.io.File;

          public class ReadFile {
            public ReadFile() {}

            /**
             * 刪除某個(gè)文件夾下的所有文件夾和文件
             * @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;
            }

            /**
             * 刪除某個(gè)文件夾下的所有文件夾和文件
             * @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");
            }

          }

          posted on 2007-06-28 15:46 imcb 閱讀(415) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 宾阳县| 成武县| 沐川县| 黔江区| 沧州市| 开远市| 和政县| 沙雅县| 云安县| 宜都市| 永定县| 曲周县| 泽库县| 北流市| 黄山市| 白水县| 定襄县| 浦江县| 泸定县| 康乐县| 桑植县| 镇原县| 尚志市| 周宁县| 成武县| 岳池县| 芦山县| 大姚县| 习水县| 西安市| 日喀则市| 永靖县| 若羌县| 泽普县| 财经| 江津市| 咸丰县| 通河县| 九龙城区| 甘泉县| 高要市|