Java技術專區--Hilly

          http://blog.duyouyou.com
             :: 首頁 :: 新隨筆 :: 聯系 ::  :: 管理

          文件或文件夾拷貝(源代碼)

          Posted on 2006-08-10 23:41 duyouyou.com 閱讀(1600) 評論(1)  編輯  收藏 所屬分類: web技術

          本人原創,轉載請注明出處http://www.aygfsteel.com/web/

          // 任務:
          // 寫一個文件拷貝函數:?fileCopy(String?a?,String?b)???
          // a--表示原文件名???b--表示目標文件名擴展:
          // 如果a是文件,則copy?a到b?;
          // 如果a是目錄,則copy?a下的所有文件和文件夾(包括子文件夾)到b目錄下。
          //

          import ?java.io. * ;


          public ? class ?IODemo?{
          ????
          ????
          public ? void ?fileCopy(String?a,?String?b){
          ????????File?file?
          = ? new ?File(a);

          ????????
          if ( ! file.exists()){
          ????????????System.out.println(a?
          + ? " ?Not?Exists. " );
          ????????????
          return ;
          ????????}
          ????????File?fileb?
          = ? new ?File(b);

          ????????
          if (file.isFile()){
          ????????????FileInputStream?fis?
          = ? null ;
          ????????????FileOutputStream?fos?
          = null ;
          ????????????
          try ?{
          ????????????????fis?
          = ? new ?FileInputStream(file);
          ????????????????fos?
          = ?? new ?FileOutputStream(fileb);
          ????????????????
          ????????????????
          byte []?bb? = new ? byte [?( int )file.length()];
          ????????????????fis.read(bb);
          ????????????????fos.write(bb);

          ????????????}
          catch ?(IOException?e){
          ????????????????e.printStackTrace();
          ????????????}
          finally {
          ????????????????
          try ?{
          ????????????????????fis.close();
          ????????????????????fos.close();
          ????????????????}?
          catch ?(IOException?e)?{
          ????????????????????e.printStackTrace();
          ????????????????}
          ????????????}
          ????????}
          else ? if (file.isDirectory()){
          ????????????
          if ( ! fileb.exists()){
          ????????????????fileb.mkdir();
          ????????????}
          ????????????String[]?fileList;
          ????????????fileList?
          = ?file.list();
          ????????????
          for ( int ?i? = ? 0 ;?i? < ?fileList.length;?i ++ ){
          ????????????????fileCopy(a?
          + ? " \\ " ? + ?fileList[i],b? + ? " \\ " ? + ?fileList[i]);
          ????????????}
          ????????}

          ????}
          ????
          }

          評論

          # re: 文件或文件夾拷貝(源代碼)  回復  更多評論   

          2006-08-12 12:31 by 游子
          頂一下。
          Hold住
          主站蜘蛛池模板: 彭阳县| 维西| 手游| 登封市| 巴南区| 诸城市| 盐山县| 沐川县| 清徐县| 嘉荫县| 灵山县| 思南县| 旬阳县| 峨眉山市| 无极县| 澎湖县| 买车| 游戏| 阿克| 定西市| 安康市| 双峰县| 巢湖市| 朔州市| 曲水县| 荆州市| 济宁市| 永济市| 黔西县| 山阴县| 平乐县| 司法| 龙南县| 石嘴山市| 临湘市| 海伦市| 井冈山市| 玉门市| 阿图什市| 北安市| 阿拉善右旗|