隨筆-204  評論-90  文章-8  trackbacks-0
          1.使用FileInputStream和FileOutputStream,代碼如下:sFlieName和tFileName分別為源文件(帶路徑)和目標文件(帶路徑)
          try?{
          ????
          //?Save?as?File
          ????FileInputStream?input?=?new?FileInputStream(new?File(
          ????????????sFileName));
          ????FileOutputStream?output?
          =?new?FileOutputStream(
          ????????????
          new?File(tFileName));
          ????
          int?b;
          ????
          while?(true)?{
          ????????
          if?(input.available()?<?1024)?{
          ????????????
          while?((b?=?input.read())?!=?-1)?{
          ????????????????output.write(b);
          ????????????}

          ????????????
          break;
          ????????}
          ?else?{
          ????????????input.read(buffer);
          ????????????output.write(buffer);
          ????????}

          ????}

          ????input.close();
          ????output.close();
          ????
          continue;
          }
          ?catch?(FileNotFoundException?e)?{
          ????e.printStackTrace();
          }
          ?catch?(IOException?e)?{
          ????e.printStackTrace();
          }
          2.使用FileChennal,本人認為這種方法在文件移動,不做別的處理時很好用,又好看,哈哈,特別是它的transferTo(),很棒啊,想了解看文檔啊,sFileName和tFileName同上
          try?{
          ????
          //?Save?as?File
          ????FileInputStream?input?=?new?FileInputStream(sFileName);????????????????????
          ????FileOutputStream?output?
          =?new?FileOutputStream(tFileName);
          ????FileChannel?infileChannel?
          =?input.getChannel();???
          ????FileChannel?outfileChannel?
          =?output.getChannel();???
          ????
          long?size?=?infileChannel.size();???
          ????infileChannel.transferTo(
          0,?size,?(WritableByteChannel)outfileChannel);????
          ????
          continue;
          }
          ?catch?(FileNotFoundException?e)?{
          ????e.printStackTrace();
          }
          ?catch?(IOException?e)?{
          ????e.printStackTrace();
          }

          posted on 2006-07-05 16:21 一凡 閱讀(5145) 評論(0)  編輯  收藏 所屬分類: JAVA 基礎
          主站蜘蛛池模板: 富蕴县| 隆昌县| 阿尔山市| 平江县| 石台县| 基隆市| 怀柔区| 和政县| 南宁市| 中西区| 福安市| 内乡县| 电白县| 泸水县| 疏附县| 政和县| 九江县| 临清市| 龙海市| 安达市| 娱乐| 黎平县| 阜新| 白水县| 德钦县| 蕲春县| 永吉县| 阿图什市| 华池县| 清新县| 舞阳县| 正定县| 雅江县| 葫芦岛市| 丰县| 苗栗县| 望都县| 利辛县| 虞城县| 远安县| 赫章县|