生活随笔
收集整理的這篇文章主要介紹了
写入指定长度的字节到文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用Java?,如何把指定長度的字節寫入文件呢,或者說如何從inputStream 中讀取指定長度的字節寫入outputStream中呢?
Java代碼??
??????????????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)?{??????????????length_tmp?-=?readSize;????????????????outs.write(bytes,?0,?readSize);??????????????if?(length_tmp?==?0)?{??????????????????break;??????????????}?????????????????????????????if?(length_tmp?<?SystemHWUtil.BUFF_SIZE)?{???????????????bytes?=?new?byte[(int)?length_tmp];???????????????}??????????}??????????outs.flush();??????????if?(isCloseOutputStream)?{??????????????outs.close();??????????}??????????return?outs;??????}??????????????????public?static?void?writeFromFile2File(InputStream?fin,?OutputStream?fout,??????????????long?length2)?throws?IOException?{????????????????????if?(length2?==?0)?{????????????????????????????????????????????????????????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];???????????????}??????????}????????}????????????????????????public?static?void?writeFromFile2File(FileInputStream?fin,?File?outPutFile,??????????????long?length2,?boolean?append)?throws?IOException?{????????????????????if?(length2?==?0)?{??????????????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();??????????}??????}?? ?上述代碼見附件中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代碼??
?????????????private?static?void?writeStubFile(String?content,?String?charset,??File?file)?throws?IOException?{??????????FileWriterWithEncoding?fileW?=?new?FileWriterWithEncoding(file,?charset);??????????fileW.write(content);??????????fileW.close();????????????????} ?
總結
以上是生活随笔為你收集整理的写入指定长度的字节到文件的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。