開花流水

          空山無人,水流花開。

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            79 Posts :: 42 Stories :: 160 Comments :: 0 Trackbacks

          /**
            * 轉移文件路徑
            *
            * @param filename
            *            文件名
            * @param oldpath
            *            舊文件路徑
            * @param newpath
            *            新文件路徑
            * @param cover
            *            若新目錄下存在和轉移文件具有相同文件名的文件時,是否覆蓋新目錄下文件,cover=true將會覆蓋原文件,否則不操作
            */
           public void changeDirectory(String fileName, String oldPath,
             String newPath, boolean cover) {
            if (!oldPath.equals(newPath)) {
             File oldFile = new File(oldPath + "/" + fileName);
             File newFile = new File(newPath + "/" + fileName);
             if (newFile.exists()) {
              if (cover) {
               newFile.delete();
               oldFile.renameTo(newFile);
              } else {
               System.out.println("新目錄已經存在:" + fileName);
              }
             } else {
              oldFile.renameTo(newFile);
             }
            }
           }

           /**
            * 文件重命名
            *
            * @param path
            *            文件目錄
            * @param oldname
            *            原來的文件名
            * @param newname
            *            新文件名
            */
           public void renameFile(String path, String oldname, String newname) {
            if (!oldname.equals(newname)) {
             File oldfile = new File(path + "/" + oldname);
             File newfile = new File(path + "/" + newname);
             if (newfile.exists()) {// 若在該目錄下已經有一個文件和新文件名相同,則刪除重建
              System.out.println(newname + "已經存在!");
              newfile.delete();
              oldfile.renameTo(newfile);
             } else {
              oldfile.renameTo(newfile);
             }
            }
           }

           /**
            * 創建文件夾
            *
            * @param path
            *            文件路徑
            */
           public void createDir(String path) {
            String pathes[] = path.split("/");
            String filePath = "";
            for (int i = 0; i < pathes.length; i++) {
             filePath += pathes[i];
             filePath += "/";
             File dirPath = new File(filePath);
             if (!dirPath.exists())
              dirPath.mkdir();
            }
           }

          posted on 2009-10-22 16:11 開花流水 閱讀(255) 評論(0)  編輯  收藏 所屬分類: java技術
          主站蜘蛛池模板: 斗六市| 罗源县| 常德市| 商丘市| 上犹县| 望都县| 万山特区| 会东县| 大安市| 虎林市| 错那县| 湟中县| 棋牌| 武义县| 鹤峰县| 阳江市| 天峨县| 兴宁市| 清流县| 德化县| 望城县| 桐庐县| 晋州市| 曲沃县| 美姑县| 七台河市| 拜城县| 铜梁县| 清徐县| 化隆| 惠水县| 诸暨市| 白山市| 长沙县| 马关县| 五常市| 宾阳县| 丰都县| 历史| 承德县| 陈巴尔虎旗|