隨筆-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 一凡 閱讀(5141) 評論(0)  編輯  收藏 所屬分類: JAVA 基礎
          主站蜘蛛池模板: 汶上县| 来宾市| 田林县| 尚义县| 无棣县| 乌拉特前旗| 邹城市| 凌源市| 高邮市| 西华县| 南江县| 信宜市| 万盛区| 巨野县| 集安市| 桐乡市| 运城市| 高台县| 奇台县| 东兰县| 安图县| 泽普县| 鄱阳县| 山阴县| 若尔盖县| 滨州市| 赫章县| 大港区| 大邑县| 买车| 枞阳县| 屏东县| 青田县| 郧西县| 外汇| 山西省| 阳高县| 上高县| 阳西县| 湄潭县| 乡城县|