日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

写入指定长度的字节到文件

發布時間:2024/4/13 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 写入指定长度的字节到文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用Java?,如何把指定長度的字節寫入文件呢,或者說如何從inputStream 中讀取指定長度的字節寫入outputStream中呢?

Java代碼??
  • /***?
  • ?????*?write?inputstream?into?file?according?to?specified?length.?
  • ?????*??
  • ?????*?@param?file?
  • ?????*?@param?ins?
  • ?????*????????????:?not?closed?
  • ?????*?@param?length2?
  • ?????*?@throws?IOException?
  • ?????*/??
  • ????public?static?FileOutputStream?writeInputStream2File(File?file,??
  • ????????????InputStream?ins,?long?length2,?boolean?isCloseOutputStream)??
  • ????????????throws?IOException?{??
  • ????????String?parentDir?=?SystemHWUtil.getParentDir(file.getAbsolutePath());??
  • ????????File?fatherFile?=?new?File(parentDir);??
  • ????????if?(!fatherFile.exists())?{??
  • ????????????fatherFile.mkdirs();??
  • ????????}??
  • ????????FileOutputStream?outs?=?new?FileOutputStream(file);??
  • ????????int?readSize;??
  • ????????byte[]?bytes?=?null;??
  • ????????bytes?=?new?byte[(int)?length2];??
  • ??
  • ????????long?length_tmp?=?length2;??
  • ????????while?((readSize?=?ins.read(bytes))?!=?SystemHWUtil.NEGATIVE_ONE/*-1*/)?{??
  • ????????????length_tmp?-=?readSize;??
  • ??
  • ????????????outs.write(bytes,?0,?readSize);??
  • ????????????if?(length_tmp?==?0)?{??
  • ????????????????break;??
  • ????????????}??
  • ????????????//非常重要,千萬不能去掉!!!??
  • ?????????????if?(length_tmp?<?SystemHWUtil.BUFF_SIZE/*4096*/)?{??
  • ?????????????bytes?=?new?byte[(int)?length_tmp];??
  • ?????????????}??
  • ????????}??
  • ????????outs.flush();??
  • ????????if?(isCloseOutputStream)?{??
  • ????????????outs.close();??
  • ????????}??
  • ????????return?outs;??
  • ????}??
  • ??
  • /***?
  • ?????*?Not?responsible?for?closing?the?output?and?input?stream?寫入指定長度的字節到輸出流?
  • ?????*??
  • ?????*?@param?fin?
  • ?????*?@param?fout?
  • ?????*????????????:?The?divided?file?
  • ?????*?@param?length2?
  • ?????*?@throws?IOException?
  • ?????*/??
  • ????public?static?void?writeFromFile2File(InputStream?fin,?OutputStream?fout,??
  • ????????????long?length2)?throws?IOException?{??
  • ??????????
  • ????????if?(length2?==?0)?{//?want?to?write?zero?bytes??
  • ????????????//?if?(fout?!=?null)?{??
  • ????????????//?fout.close();??
  • ????????????//?}??
  • ????????????return;??
  • ????????}??
  • ????????int?readSize;??
  • ????????byte[]?bytes?=?null;??
  • ????????if?(length2?>=?SystemHWUtil.BUFF_SIZE)?{??
  • ????????????bytes?=?new?byte[SystemHWUtil.BUFF_SIZE];??
  • ????????}?else?{??
  • ????????????bytes?=?new?byte[(int)?length2];??
  • ????????}??
  • ??
  • ????????long?length_tmp?=?length2;??
  • ????????while?((readSize?=?fin.read(bytes))?!=?SystemHWUtil.NEGATIVE_ONE)?{??
  • ??????????????
  • ????????????length_tmp?-=?readSize;??
  • ????????????fout.write(bytes,?0,?readSize);??
  • ????????????if?(length_tmp?==?0)?{??
  • ????????????????break;??
  • ????????????}??
  • ????????????//非常重要,千萬不能刪除??
  • ?????????????if?(length_tmp?<?SystemHWUtil.BUFF_SIZE)?{??
  • ?????????????bytes?=?new?byte[(int)?length_tmp];??
  • ?????????????}??
  • ????????}??
  • ??
  • ????}??
  • ??
  • ????/***?
  • ?????*?Responsible?for?closing?the?output?stream?
  • ?????*??
  • ?????*?@param?fin?
  • ?????*?@param?outPutFile?
  • ?????*?@param?length2?
  • ?????*????????????:The?number?of?bytes?to?be?written?
  • ?????*?@param?append?
  • ?????*????????????:?Whether?additional?
  • ?????*?@throws?IOException?
  • ?????*/??
  • ????public?static?void?writeFromFile2File(FileInputStream?fin,?File?outPutFile,??
  • ????????????long?length2,?boolean?append)?throws?IOException?{??
  • ??????????
  • ????????if?(length2?==?0)?{//?want?to?write?zero?bytes??
  • ????????????return;??
  • ????????}??
  • ????????FileOutputStream?fout?=?null;??
  • ????????try?{??
  • ????????????fout?=?new?FileOutputStream(outPutFile,?append/*?追加?*/);??
  • ????????}?catch?(FileNotFoundException?e1)?{??
  • ????????????e1.printStackTrace();??
  • ????????}??
  • ????????try?{??
  • ????????????writeFromFile2File(fin,?fout,?length2);??
  • ????????}?catch?(IOException?e)?{??
  • ????????????e.printStackTrace();??
  • ????????}?finally?{??
  • ????????????fout.flush();??
  • ????????????fout.close();//?Close?the?stream??
  • ????????}??
  • ????}??
  • ?上述代碼見附件中io0007-find_progess\src\main\java\com\io\hw\file\util\FileUtils.java

    依賴的包:is_chinese-0.0.2-SNAPSHOT.jar

    參考:http://hw1287789687.iteye.com/blog/2023095

    寫入文件:

    Java代碼??
  • /***?
  • ?????*?寫入文件?
  • ?????*?@param?content?
  • ?????*?@param?charset?
  • ?????*?@param?readAndWriteResult?
  • ?????*?@param?file?
  • ?????*?@throws?IOException?
  • ?????*/??
  • ????private?static?void?writeStubFile(String?content,?String?charset,??File?file)?throws?IOException?{??
  • ????????FileWriterWithEncoding?fileW?=?new?FileWriterWithEncoding(file,?charset);??
  • ????????fileW.write(content);??
  • ????????fileW.close();??
  • ??????????
  • ????} ?
  • 總結

    以上是生活随笔為你收集整理的写入指定长度的字节到文件的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。