笑看風(fēng)云

          一切從頭開始
          posts - 28, comments - 1, trackbacks - 0, articles - 2

           

            1package test;
            2
            3import java.io.BufferedReader;
            4import java.io.File;
            5import java.io.FileInputStream;
            6import java.io.FileOutputStream;
            7import java.io.FileReader;
            8
            9import junit.framework.TestCase;
           10
           11public class Filetest extends TestCase {
           12    
           13    // 創(chuàng)建文件夾
           14    public String  creatdir(String dir) {
           15        File dirFile;
           16        boolean bFile;
           17
           18        bFile = false;
           19        //指定創(chuàng)建文件夾的目錄和名稱
           20        dirFile = new File(dir);
           21        bFile = dirFile.exists();
           22
           23        if (bFile == true{
           24            System.out.println("文件夾已經(jīng)存在.");
           25        }
           else {
           26            System.out.println("文件夾不存在,開始創(chuàng)建");
           27            bFile = dirFile.mkdirs();
           28            if (bFile == true{
           29                System.out.println("創(chuàng)建文件夾成功!");
           30            }
           else {
           31                System.out.println("創(chuàng)建文件夾失敗.");
           32                System.exit(1);
           33            }

           34        }

           35        
           36        return dir;
           37    }

           38
           39    //拷貝文件夾
           40    public void copyDir(File from, File to) {
           41        // 判斷需要拷貝到的路徑下的文件夾是否存在
           42        if (!to.exists()) {
           43            // 創(chuàng)建文件夾
           44            to.mkdirs();
           45        }

           46        File[] files = from.listFiles();
           47        for (int i = 0; i < files.length; i++{//遍歷文件夾
           48            File file1 = files[i];
           49            File file2 = new File(to.getPath() + File.separator
           50                    + files[i].getName());
           51            if (!file1.isDirectory()) //拷貝文件
           52                copyFile(file1, file2);
           53            }
           else {
           54                copyDir(file1, file2);
           55            }

           56        }

           57
           58    }

           59    
           60    //拷貝文件
           61    public void copyFile(File src, File dest) {
           62        try {
           63            System.out.println(src.getAbsoluteFile() + " -> "
           64                    + dest.getAbsoluteFile());
           65            FileInputStream in = new FileInputStream(src);
           66            FileOutputStream out = new FileOutputStream(dest);
           67            byte[] buffer = new byte[1024];
           68            while (in.read(buffer) != -1{
           69                out.write(buffer);
           70            }

           71            out.close();
           72            in.close();
           73            System.out.println("文件拷貝成功");
           74        }
           catch (Exception e) {
           75            System.out.println("文件拷貝失敗");
           76        }

           77
           78    }

           79
           80    // 刪除文件夾
           81    // param folderPath 文件夾完整絕對(duì)路徑
           82    public void delFolder(String folderPath) {
           83        try {
           84            this.delAllFile(folderPath); // 刪除完里面所有內(nèi)容(第一步)
           85            String filePath = folderPath;
           86            filePath = filePath.toString();
           87            java.io.File myFilePath = new java.io.File(filePath);
           88            myFilePath.delete(); // 刪除空文件夾(第二步)
           89        }
           catch (Exception e) {
           90            e.printStackTrace();
           91        }

           92    }

           93
           94    // 刪除指定文件夾下所有文件(第一步)
           95    // param path 文件夾完整絕對(duì)路徑
           96    public boolean delAllFile(String path) {
           97        boolean flag = false;
           98        File file = new File(path);
           99        // 文件夾是否存在
          100        if (!file.exists()) {
          101            return flag;
          102        }

          103        // 如果不是目錄
          104        if (!file.isDirectory()) {
          105            return flag;
          106        }

          107        String[] tempList = file.list();
          108        File temp = null;
          109        for (int i = 0; i < tempList.length; i++{//遍歷所有文件夾以及里面的文件
          110            if (path.endsWith(File.separator)) {  //文件分隔符,各個(gè)操作系統(tǒng)不一樣,如WIndows的是"\",而Linux的是"/"
          111                temp = new File(path + tempList[i]);
          112            }
           else {
          113                temp = new File(path + File.separator + tempList[i]);
          114            }

          115            if (temp.isFile()) {//刪除文件
          116                temp.delete();
          117            }

          118            if (temp.isDirectory()) {
          119                delAllFile(path + "/" + tempList[i]);// 先刪除文件夾里面的文件
          120                delFolder(path + "/" + tempList[i]);// 再刪除空文件夾
          121                flag = true;
          122            }

          123        }

          124        return flag;
          125    }

          126
          127    //讀文件內(nèi)容
          128    public File readfile(String path) {
          129        BufferedReader bufread;
          130        String read = "";
          131        String readStr = "";
          132        File file = new File(path);
          133        try {
          134            FileReader fileread = new FileReader(file);
          135            bufread = new BufferedReader(fileread);
          136            while ((read = bufread.readLine()) != null{
          137                readStr = readStr + read;
          138            }

          139            bufread.close();
          140        }
           catch (Exception d) {
          141            System.out.println(d.getMessage());
          142        }

          143        System.out.println("###readStr:" + readStr);
          144        return file;
          145    }

          146
          147    public void testcopyfile() {
          148
          149        Filetest t = new Filetest();
          150        String a = "D://test1";
          151        String b = "D://test2";
          152      // t.copyDir(t.readfile(a), t.readfile(b));
          153      // t.copyDir(new File(a), new File(b));
          154        t.delFolder(a);
          155
          156    }

          157}

          158

           

          主站蜘蛛池模板: 鱼台县| 德保县| 太原市| 获嘉县| 鄂伦春自治旗| 凤翔县| 田林县| 扎囊县| 万州区| 响水县| 盐山县| 乐至县| 镇沅| 兰西县| 湟中县| 通榆县| 山丹县| 汪清县| 昔阳县| 黔江区| 长垣县| 牙克石市| 宝兴县| 丹棱县| 余江县| 台安县| 鞍山市| 彭阳县| 舞钢市| 克什克腾旗| 志丹县| 荥阳市| 莆田市| 湟源县| 七台河市| 丘北县| 铜陵市| 芦山县| 米脂县| 山西省| 灌云县|