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

          與我一起遨游吧

           

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

          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");
            }

          }

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


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 神农架林区| 山丹县| 许昌县| 黄冈市| 股票| 寻甸| 合作市| 深水埗区| 义乌市| 哈尔滨市| 乌兰察布市| 唐河县| 南平市| 新巴尔虎右旗| 泰和县| 曲阳县| 延川县| 河源市| 丹阳市| 沭阳县| 林周县| 高唐县| 花莲县| 泰顺县| 防城港市| 陈巴尔虎旗| 古丈县| 特克斯县| 射阳县| 南澳县| 雅江县| 历史| 二连浩特市| 东至县| 山东省| 双鸭山市| 武义县| 承德市| 中西区| 乡城县| 安乡县|