posts - 78,  comments - 48,  trackbacks - 0
          ??1 /*
          ??2 ?*?Made?In?GamVan.com
          ??3 ?*?Created?on?2005年3月18日,?下午8:37
          ??4 ? */

          ??5 package ?com.gamvan.tools;
          ??6 import ?java.io.BufferedReader;
          ??7 import ?java.io.File;
          ??8 import ?java.io.FileInputStream;
          ??9 import ?java.io.FileOutputStream;
          ?10 import ?java.io.FileWriter;
          ?11 import ?java.io.IOException;
          ?12 import ?java.io.InputStream;
          ?13 import ?java.io.InputStreamReader;
          ?14 import ?java.io.PrintWriter;
          ?15 import ?java.util.StringTokenizer;
          ?16 public ? class ?FileOperate? {
          ?17 ???? private ?String?message;
          ?18 ???? public ?FileOperate()? {
          ?19 ????}

          ?20
          ?21 ???? /**
          ?22 ?????*?讀取文本文件內容
          ?23 ?????*? @param ?filePathAndName?帶有完整絕對路徑的文件名
          ?24 ?????*? @param ?encoding?文本文件打開的編碼方式
          ?25 ?????*? @return ?返回文本文件的內容
          ?26 ????? */

          ?27 ???? public ?String?readTxt(String?filePathAndName,String?encoding)? throws ?IOException {
          ?28 ?????encoding? = ?encoding.trim();
          ?29 ?????StringBuffer?str? = ? new ?StringBuffer( "" );
          ?30 ?????String?st? = ? "" ;
          ?31 ????? try {
          ?32 ??????FileInputStream?fs? = ? new ?FileInputStream(filePathAndName);
          ?33 ??????InputStreamReader?isr;
          ?34 ?????? if (encoding.equals( "" )) {
          ?35 ???????isr? = ? new ?InputStreamReader(fs);
          ?36 ??????}
          else {
          ?37 ???????isr? = ? new ?InputStreamReader(fs,encoding);
          ?38 ??????}

          ?39 ??????BufferedReader?br? = ? new ?BufferedReader(isr);
          ?40 ?????? try {
          ?41 ???????String?data? = ? "" ;
          ?42 ??????? while ((data? = ?br.readLine()) != null ) {
          ?43 ?????????str.append(data + " ? " );?
          ?44 ???????}

          ?45 ??????}
          catch (Exception?e) {
          ?46 ???????str.append(e.toString());
          ?47 ??????}

          ?48 ??????st? = ?str.toString();
          ?49 ?????}
          catch (IOException?es) {
          ?50 ??????st? = ? "" ;
          ?51 ?????}

          ?52 ????? return ?st;?????
          ?53 ????}

          ?54
          ?55 ???? /**
          ?56 ?????*?新建目錄
          ?57 ?????*? @param ?folderPath?目錄
          ?58 ?????*? @return ?返回目錄創建后的路徑
          ?59 ????? */

          ?60 ???? public ?String?createFolder(String?folderPath)? {
          ?61 ????????String?txt? = ?folderPath;
          ?62 ???????? try ? {
          ?63 ????????????java.io.File?myFilePath? = ? new ?java.io.File(txt);
          ?64 ????????????txt? = ?folderPath;
          ?65 ???????????? if ?( ! myFilePath.exists())? {
          ?66 ????????????????myFilePath.mkdir();
          ?67 ????????????}

          ?68 ????????}

          ?69 ???????? catch ?(Exception?e)? {
          ?70 ????????????message? = ? " 創建目錄操作出錯 " ;
          ?71 ????????}

          ?72 ???????? return ?txt;
          ?73 ????}

          ?74 ????
          ?75 ???? /**
          ?76 ?????*?多級目錄創建
          ?77 ?????*? @param ?folderPath?準備要在本級目錄下創建新目錄的目錄路徑?例如?c:myf
          ?78 ?????*? @param ?paths?無限級目錄參數,各級目錄以單數線區分?例如?a|b|c
          ?79 ?????*? @return ?返回創建文件后的路徑?例如?c:myfac
          ?80 ????? */

          ?81 ???? public ?String?createFolders(String?folderPath,?String?paths) {
          ?82 ????????String?txts? = ?folderPath;
          ?83 ???????? try {
          ?84 ????????????String?txt;
          ?85 ????????????txts? = ?folderPath;
          ?86 ????????????StringTokenizer?st? = ? new ?StringTokenizer(paths, " | " );
          ?87 ???????????? for ( int ?i = 0 ;?st.hasMoreTokens();?i ++ ) {
          ?88 ????????????????????txt? = ?st.nextToken().trim();
          ?89 ???????????????????? if (txts.lastIndexOf( " / " ) !=- 1 ) {?
          ?90 ????????????????????????txts? = ?createFolder(txts + txt);
          ?91 ????????????????????}
          else {
          ?92 ????????????????????????txts? = ?createFolder(txts + txt + " / " );????
          ?93 ????????????????????}

          ?94 ????????????}

          ?95 ???????}
          catch (Exception?e) {
          ?96 ???????????message? = ? " 創建目錄操作出錯! " ;
          ?97 ???????}

          ?98 ???????? return ?txts;
          ?99 ????}

          100
          101 ????
          102 ???? /**
          103 ?????*?新建文件
          104 ?????*? @param ?filePathAndName?文本文件完整絕對路徑及文件名
          105 ?????*? @param ?fileContent?文本文件內容
          106 ?????*? @return
          107 ????? */

          108 ???? public ? void ?createFile(String?filePathAndName,?String?fileContent)? {
          109 ?????
          110 ???????? try ? {
          111 ????????????String?filePath? = ?filePathAndName;
          112 ????????????filePath? = ?filePath.toString();
          113 ????????????File?myFilePath? = ? new ?File(filePath);
          114 ???????????? if ?( ! myFilePath.exists())? {
          115 ????????????????myFilePath.createNewFile();
          116 ????????????}

          117 ????????????FileWriter?resultFile? = ? new ?FileWriter(myFilePath);
          118 ????????????PrintWriter?myFile? = ? new ?PrintWriter(resultFile);
          119 ????????????String?strContent? = ?fileContent;
          120 ????????????myFile.println(strContent);
          121 ????????????myFile.close();
          122 ????????????resultFile.close();
          123 ????????}

          124 ???????? catch ?(Exception?e)? {
          125 ????????????message? = ? " 創建文件操作出錯 " ;
          126 ????????}

          127 ????}

          128
          129
          130 ???? /**
          131 ?????*?有編碼方式的文件創建
          132 ?????*? @param ?filePathAndName?文本文件完整絕對路徑及文件名
          133 ?????*? @param ?fileContent?文本文件內容
          134 ?????*? @param ?encoding?編碼方式?例如?GBK?或者?UTF-8
          135 ?????*? @return
          136 ????? */

          137 ???? public ? void ?createFile(String?filePathAndName,?String?fileContent,?String?encoding)? {
          138 ?????
          139 ???????? try ? {
          140 ????????????String?filePath? = ?filePathAndName;
          141 ????????????filePath? = ?filePath.toString();
          142 ????????????File?myFilePath? = ? new ?File(filePath);
          143 ???????????? if ?( ! myFilePath.exists())? {
          144 ????????????????myFilePath.createNewFile();
          145 ????????????}

          146 ????????????PrintWriter?myFile? = ? new ?PrintWriter(myFilePath,encoding);
          147 ????????????String?strContent? = ?fileContent;
          148 ????????????myFile.println(strContent);
          149 ????????????myFile.close();
          150 ????????}

          151 ???????? catch ?(Exception?e)? {
          152 ????????????message? = ? " 創建文件操作出錯 " ;
          153 ????????}

          154 ????}
          ?
          155
          156
          157 ???? /**
          158 ?????*?刪除文件
          159 ?????*? @param ?filePathAndName?文本文件完整絕對路徑及文件名
          160 ?????*? @return ?Boolean?成功刪除返回true遭遇異常返回false
          161 ????? */

          162 ???? public ? boolean ?delFile(String?filePathAndName)? {
          163 ????? boolean ?bea? = ? false ;
          164 ???????? try ? {
          165 ????????????String?filePath? = ?filePathAndName;
          166 ????????????File?myDelFile? = ? new ?File(filePath);
          167 ???????????? if (myDelFile.exists()) {
          168 ?????????????myDelFile.delete();
          169 ?????????????bea? = ? true ;
          170 ????????????}
          else {
          171 ?????????????bea? = ? false ;
          172 ?????????????message? = ?(filePathAndName + " <br>刪除文件操作出錯 " );
          173 ????????????}

          174 ????????}

          175 ???????? catch ?(Exception?e)? {
          176 ????????????message? = ?e.toString();
          177 ????????}

          178 ???????? return ?bea;
          179 ????}

          180 ????
          181
          182
          183 ???? /**
          184 ?????*?刪除文件夾
          185 ?????*? @param ?folderPath?文件夾完整絕對路徑
          186 ?????*? @return
          187 ????? */

          188 ???? public ? void ?delFolder(String?folderPath)? {
          189 ???????? try ? {
          190 ????????????delAllFile(folderPath);? // 刪除完里面所有內容
          191 ????????????String?filePath? = ?folderPath;
          192 ????????????filePath? = ?filePath.toString();
          193 ????????????java.io.File?myFilePath? = ? new ?java.io.File(filePath);
          194 ????????????myFilePath.delete();? // 刪除空文件夾
          195 ????????}

          196 ???????? catch ?(Exception?e)? {
          197 ????????????message? = ?( " 刪除文件夾操作出錯 " );
          198 ????????}

          199 ????}

          200 ????
          201 ????
          202 ???? /**
          203 ?????*?刪除指定文件夾下所有文件
          204 ?????*? @param ?path?文件夾完整絕對路徑
          205 ?????*? @return
          206 ?????*? @return
          207 ????? */

          208 ???? public ? boolean ?delAllFile(String?path)? {
          209 ????? boolean ?bea? = ? false ;
          210 ????????File?file? = ? new ?File(path);
          211 ???????? if ?( ! file.exists())? {
          212 ???????????? return ?bea;
          213 ????????}

          214 ???????? if ?( ! file.isDirectory())? {
          215 ???????????? return ?bea;
          216 ????????}

          217 ????????String[]?tempList? = ?file.list();
          218 ????????File?temp? = ? null ;
          219 ???????? for ?( int ?i? = ? 0 ;?i? < ?tempList.length;?i ++ )? {
          220 ???????????? if ?(path.endsWith(File.separator))? {
          221 ????????????????temp? = ? new ?File(path? + ?tempList[i]);
          222 ????????????}
          else {
          223 ????????????????temp? = ? new ?File(path? + ?File.separator? + ?tempList[i]);
          224 ????????????}

          225 ???????????? if ?(temp.isFile())? {
          226 ????????????????temp.delete();
          227 ????????????}

          228 ???????????? if ?(temp.isDirectory())? {
          229 ????????????????delAllFile(path + " / " + ?tempList[i]); // 先刪除文件夾里面的文件
          230 ????????????????delFolder(path + " / " + ?tempList[i]); // 再刪除空文件夾
          231 ????????????????bea? = ? true ;
          232 ????????????}

          233 ????????}

          234 ???????? return ?bea;
          235 ????}

          236
          237
          238 ???? /**
          239 ?????*?復制單個文件
          240 ?????*? @param ?oldPathFile?準備復制的文件源
          241 ?????*? @param ?newPathFile?拷貝到新絕對路徑帶文件名
          242 ?????*? @return
          243 ????? */

          244 ???? public ? void ?copyFile(String?oldPathFile,?String?newPathFile)? {
          245 ???????? try ? {
          246 ???????????? int ?bytesum? = ? 0 ;
          247 ???????????? int ?byteread? = ? 0 ;
          248 ????????????File?oldfile? = ? new ?File(oldPathFile);
          249 ???????????? if ?(oldfile.exists())? {? // 文件存在時
          250 ????????????????InputStream?inStream? = ? new ?FileInputStream(oldPathFile);? // 讀入原文件
          251 ????????????????FileOutputStream?fs? = ? new ?FileOutputStream(newPathFile);
          252 ???????????????? byte []?buffer? = ? new ? byte [ 1444 ];
          253 ???????????????? while ((byteread? = ?inStream.read(buffer))? != ? - 1 ) {
          254 ????????????????????bytesum? += ?byteread;? // 字節數?文件大小
          255 ????????????????????System.out.println(bytesum);
          256 ????????????????????fs.write(buffer,? 0 ,?byteread);
          257 ????????????????}

          258 ????????????????inStream.close();
          259 ????????????}

          260 ????????}
          catch ?(Exception?e)? {
          261 ????????????message? = ?( " 復制單個文件操作出錯 " );
          262 ????????}

          263 ????}

          264 ????
          265
          266 ???? /**
          267 ?????*?復制整個文件夾的內容
          268 ?????*? @param ?oldPath?準備拷貝的目錄
          269 ?????*? @param ?newPath?指定絕對路徑的新目錄
          270 ?????*? @return
          271 ????? */

          272 ???? public ? void ?copyFolder(String?oldPath,?String?newPath)? {
          273 ???????? try ? {
          274 ???????????? new ?File(newPath).mkdirs();? // 如果文件夾不存在?則建立新文件夾
          275 ????????????File?a = new ?File(oldPath);
          276 ????????????String[]?file = a.list();
          277 ????????????File?temp = null ;
          278 ???????????? for ?( int ?i? = ? 0 ;?i? < ?file.length;?i ++ )? {
          279 ???????????????? if (oldPath.endsWith(File.separator)) {
          280 ????????????????????temp = new ?File(oldPath + file[i]);
          281 ????????????????}
          else {
          282 ????????????????????temp = new ?File(oldPath + File.separator + file[i]);
          283 ????????????????}

          284 ???????????????? if (temp.isFile()) {
          285 ????????????????????FileInputStream?input? = ? new ?FileInputStream(temp);
          286 ????????????????????FileOutputStream?output? = ? new ?FileOutputStream(newPath? + ? " / " ? +
          287 ????????????????????(temp.getName()).toString());
          288 ???????????????????? byte []?b? = ? new ? byte [ 1024 ? * ? 5 ];
          289 ???????????????????? int ?len;
          290 ???????????????????? while ?((len? = ?input.read(b))? != ? - 1 )? {
          291 ????????????????????????output.write(b,? 0 ,?len);
          292 ????????????????????}

          293 ????????????????????output.flush();
          294 ????????????????????output.close();
          295 ????????????????????input.close();
          296 ????????????????}

          297 ???????????????? if (temp.isDirectory()) { // 如果是子文件夾
          298 ????????????????????copyFolder(oldPath + " / " + file[i],newPath + " / " + file[i]);
          299 ????????????????}

          300 ????????????}

          301 ????????}
          catch ?(Exception?e)? {
          302 ????????????message? = ? " 復制整個文件夾內容操作出錯 " ;
          303 ????????}

          304 ????}

          305
          306
          307 ???? /**
          308 ?????*?移動文件
          309 ?????*? @param ?oldPath
          310 ?????*? @param ?newPath
          311 ?????*? @return
          312 ????? */

          313 ???? public ? void ?moveFile(String?oldPath,?String?newPath)? {
          314 ????????copyFile(oldPath,?newPath);
          315 ????????delFile(oldPath);
          316 ????}

          317 ????
          318
          319 ???? /**
          320 ?????*?移動目錄
          321 ?????*? @param ?oldPath
          322 ?????*? @param ?newPath
          323 ?????*? @return
          324 ????? */

          325 ???? public ? void ?moveFolder(String?oldPath,?String?newPath)? {
          326 ????????copyFolder(oldPath,?newPath);
          327 ????????delFolder(oldPath);
          328 ????}

          329 ???? public ?String?getMessage() {
          330 ???????? return ? this .message;
          331 ????}

          332 }

          333
          334
          335
          336 ??
          posted on 2006-07-27 13:03 黑咖啡 閱讀(330) 評論(0)  編輯  收藏 所屬分類: Tec Article

          <2006年7月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          留言簿(2)

          隨筆分類(67)

          文章分類(43)

          Good Article

          Good Blogs

          Open Source

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 营口市| 宝坻区| 红安县| 定结县| 双牌县| 灵山县| 千阳县| 隆德县| 黔西| 吉木萨尔县| 秀山| 伊金霍洛旗| 西平县| 盐山县| 华坪县| 漳浦县| 秀山| 美姑县| 高碑店市| 惠州市| 临漳县| 治县。| 隆尧县| 临邑县| 兴安县| 司法| 陈巴尔虎旗| 岳阳县| 安宁市| 花莲县| 元谋县| 东乡族自治县| 东山县| 永清县| 巴楚县| 永德县| 新乡县| 天长市| 麻阳| 德庆县| 铁岭县|