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

          與我一起遨游吧

           

          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)  編輯  收藏


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


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

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 蒲江县| 鹤岗市| 济阳县| 青河县| 衡东县| 南投县| 奉新县| 肥乡县| 眉山市| 彭阳县| 天长市| 琼结县| 吴忠市| 桐乡市| 宜春市| 东海县| 宁国市| 卢氏县| 泗水县| 忻城县| 六枝特区| 驻马店市| 循化| 特克斯县| 靖边县| 个旧市| 孙吴县| 夏河县| 永新县| 巴马| 平邑县| 河东区| 浏阳市| 伽师县| 翁牛特旗| 莱西市| 化州市| 萨迦县| 东丽区| 迭部县| 咸阳市|