開花流水

          空山無人,水流花開。

          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技術
          主站蜘蛛池模板: 綦江县| 连山| 巨野县| 福鼎市| 莱州市| 吴桥县| 庆元县| 北碚区| 黄龙县| 扎鲁特旗| 仙游县| 连州市| 新宁县| 武陟县| 邓州市| 沛县| 图木舒克市| 广丰县| 徐汇区| 阿合奇县| 阿图什市| 扶绥县| 依安县| 彝良县| 临颍县| 敦煌市| 平顶山市| 博野县| 宜州市| 时尚| 临颍县| 泸水县| 贡山| 巴彦县| 上犹县| 茶陵县| 曲松县| 韩城市| 深水埗区| 大安市| 禹城市|