版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
JavaSE02IOUtils——saveBytes問方創(chuàng)建IOUtils類,在類中添加saveBytes(Filesrc,byte[]bs)方法,參數(shù)src表示到的文件,參數(shù)bs表示要保fos=new調(diào)用文件輸出流的write(byte[]b)方法,將b.length個字節(jié)從byte數(shù)組b寫入文件輸注意:流使用完畢需要進(jìn)行關(guān)閉實系統(tǒng)代碼實現(xiàn)如把字節(jié)數(shù)組中數(shù) 到文件@param 到的文@parambs*@throwspublicstaticvoidsaveBytes(Filesrc,byte[]}finallyif(fos!= }throwsIOExceptionFileOutputStreamfos=null;try{fos=new在IOUtils工具中,已經(jīng)完成了把字節(jié)數(shù)組中數(shù)據(jù)到文件中的方法saveBytes,現(xiàn)需要使用JUnit測試該法byte[]bs={1,2,3,4,5IOUtils.saveBytes(newFile("d:/temp/bytes.dat"),系統(tǒng)代碼實現(xiàn)如publicvoid{trybyte[]bs={1,2,3,4,5}catch(IOExceptione)}IOUtils.saveBytes(newFile("d:/temp/bytes.dat"),}IOUtils——loadBytes問以字節(jié)的方式文件中的數(shù)據(jù),到字節(jié)數(shù)組方在IOUtils類中添加loadBytes(Filesrc),src表示要的文件publicstaticbyte[]loadBytes(Filesrc)throwsIOExceptionfis=new調(diào)用文件輸入流的read(byte[]b)方法,從文件輸入流中將b.length個字節(jié)的數(shù)據(jù)讀入一個byte數(shù)組中byte[]bs=new實系統(tǒng)代碼實現(xiàn)如*以字節(jié)的方 文件中的數(shù)據(jù) 到字節(jié)數(shù)*@paramsrc*@return*@throwspublicstaticbyte[]loadBytes(Filesrc)throws{ifthrownewIllegalArgumentException("源文件+"不存在FileInputStreamfis=null;try{fis=newbyte[]bs=newreturn}finallyif(fis!=null)}}擴(kuò)在Iil實踐中,已經(jīng)完成了以字節(jié)的方式文件中的數(shù)據(jù),到字節(jié)數(shù)組的方法ladBe,現(xiàn)需要使用Jnit測試該方法。bytes.dat文件,使用Arrays.toString方法查看返回的數(shù)組的內(nèi)容publicvoid{trybyte[]bsIOUtils.loadBytes(newFile("d:/temp/bytes.dat"));}catch(IOException{}}:控制臺輸出[1,2,3,4,IOUtils——copy問方在IOUtils類中,添加copy(InputStreamis,OutputStreamos)方法,is表示字節(jié)輸入流,os表示字節(jié)輸流publicstaticvoidcopy(InputStreamis,OutputStream使用輸入流的read(byte[]b)方法,從輸入流中數(shù)據(jù),并將其在緩沖數(shù)組b中,該方法返回讀入緩沖區(qū)的總字節(jié)數(shù),如果因為已經(jīng)到達(dá)流末尾,則返回-1;使用輸出流的write(byte[]b,intoff,intlen)將指定byte數(shù)組b中從偏移量off開始的len個字節(jié)寫入輸出流。while((b=is.read(buffer))!=-1)os.write(buffer,0,}實使用使用字節(jié)流實現(xiàn)@paramis字節(jié)輸入@paramis字節(jié)輸入*@throwspublicstaticvoidcopy(InputStreamis,OutputStreamIOExceptionbyte[]buffer=newbyte[8*intb=-while((b=is.read(buffer))!=-{os.write(buffer,0,}}擴(kuò)在Iil實踐中,已經(jīng)完成了字節(jié)輸入流和字節(jié)輸出流實現(xiàn)文件的拷貝功能的方法cp,現(xiàn)需要使用Jni測試該方法。publicvoid{FileInputStreamfis=FileOutputStreamfos=tryfis=newfos=newFileOutputStream("d:/temp/names1.txt");IOUtils.copy(fis,fos);}catch(IOException{}{tryif(fis!=null)if(fos!=}catch(IOExceptione)}}}IOUtils——copyFile問方在IOUtils類中添加copyFile(Filesrc,Filedest)方法,實現(xiàn)緩沖字節(jié)流實現(xiàn)文件的拷貝,其中參數(shù)src表示要拷貝publicstaticvoidcopyFile(Filesrc,Filedest)bis=newBufferedInputStream(newos=newBufferedOutputStream(new使用BuffeedInputeam類的ead方法要拷貝的文件的數(shù)據(jù);使用Buffeeduputeam的wie的方法向目標(biāo)文件中寫入數(shù)據(jù)。循環(huán)上述過程直到要拷貝的文件內(nèi)容全部寫入目標(biāo)文件。while((b=bis.read())!=-1)}實系統(tǒng)代碼實現(xiàn)如使用緩沖字節(jié)流實現(xiàn)文件的@paramsrc文@paramdest@throwspublicstaticvoidcopyFile(Filesrc,FilethrowsIOExceptionif(!src.exists())thrownewIllegalArgumentException("源文件 +src在 BufferedInputStreambis=BufferedOutputStreambos=trybis=newnewbos=newnewintb=-while((b=bis.read())!=-1) {try(bis!=(bos!=}catch(Exceptione)}}}擴(kuò)在IOUtils實踐中,已經(jīng)完成了緩沖字節(jié)流實現(xiàn)文件的拷貝功能的方法copyFile,現(xiàn)需要使用JUnit法publicvoid{tryIOUtils.copyFile(newnew}catch(IOException{}}IOUtils——saveDouble和問double數(shù)據(jù)方在IOUtils類中添加saveDouble(doubledata,OutputStreamdest)方法,其中參數(shù)data表示要寫入基本輸publicstaticvoidsaveDouble(doubledata,OutputStreamthrowsIOException使用Daauputeam來實現(xiàn)將duble數(shù)據(jù)寫入輸出流中,Daauputeam類的wieDouble方法將寫出duble數(shù)據(jù)。DataOutputStreamdos=newpublicvoid{trydoubled=FileOutputStreamfosnewFileOutputStream("d:/temp/data.dat");IOUtils.saveDouble(d,fos);}catch(IOException{}} publicstaticdoubleloadDouble(InputStreamthrowsIOExceptionDataInputStreamdis=newdoubled=使用Jnit測試ladDuble方法。在TeIil類中添加eLadDouble方法,將aeDuble方法寫入的數(shù)據(jù)讀出來。publicvoid{tryFileInputStreamfosnewdoubled=}catch(IOException{}}:控制臺輸出實*增強(qiáng)OutputStream*增強(qiáng)OutputStream寫出double數(shù)@paramdatadouble@paramdest@throwspublicstaticvoidsaveDouble(doubledata,OutputStreamthrowsIOExceptionDataOutputStreamdos=new}*增強(qiáng) @paramsrc@return從輸入流 @throwspublicstaticdoubleloadDouble(InputStreamthrowsIOExceptionDataInputStreamdis=newDataInputStream(src);doubled=dis.readDouble();return}擴(kuò)如何向基本輸出流(OutputStream)中寫入int數(shù)據(jù);如何從基本輸入流(InputStream)中,int據(jù)@paramdataint*@paramdest出*@throwspublicstaticvoidsaveInt(intdata,OutputStreamIOExceptionDataOutputStreamdos=new}*增強(qiáng)@paramsrc@return從輸入流 @throwspublicstaticintloadInt(InputStreamthrowsIOExceptionDataInputStreamdis=newintd=dis.readInt();returnd;}publicvoid{tryintd=FileOutputStreamfosnewFileOutputStream("d:/temp/data.dat");IOUtils.saveInt(d,fos);}catch(IOException{}}publicvoid{tryFileInputStreamfosnewintd=}catch(IOException{}}IOUtils——saveLines問 如何從基本輸入流中成行文本存入字節(jié)數(shù)組回方在IOUtils類中添加saveLines(String[]lines,OutputStreamsrc)方法,其中參數(shù)lines表示字符串?dāng)?shù)據(jù),參數(shù)publicstaticvoidsaveLines(String[]lines,OutputStreamthrowsIOException構(gòu)造Buffeedie類的對象時,使用到類uputeamier,uputeamWier是字符輸出流通向字節(jié)輸出流的橋梁,使用upuSeamier(upueams)構(gòu)造方法,來構(gòu)造uputeamWier類的對象。BufferedWriterbwnewBufferedWriter(newOutputStreamWriter(src));if(lines!=null&&lines.length>0)for(inti=0;i<lines.length-1;}} publicstaticvoidsaveLines(String[]lines,OutputStreamthrowsIOException構(gòu)造BufferedReader類的對象,該類有readLine方法實現(xiàn)一個文本行,如果已到達(dá)流末尾,則返null。在構(gòu)造BufferedReader對象時,使用到InputStreamReader,InputStreamReader是字符輸入流通向節(jié)輸入流的橋梁,使用InputStreamReader(InputStreamis)構(gòu)造方法,來構(gòu)造InputStreamReader類的對象List<String>lineList=newStringline=while((line=br.readLine())!={}實系統(tǒng)代碼實現(xiàn)如*將字符串?dāng)?shù)組中的每一項內(nèi)容按行寫*@paramlines*@paramsrc*@throwspublicstaticvoidsaveLines(String[]lines,OutputStreamthrowsIOExceptionBufferedWriterbwBufferedWriter(newif(lines!=null&&lines.length>0)for(inti=0;i<lines.length-1;i++) bw.write(lines[lines.length- *從基本輸入流中成 文本存入字節(jié)數(shù)組返*@paramsrc基本輸入*@return從輸入流 *@throwspublicstaticString[]loadLines(InputStreamthrowsIOExceptionBufferedReaderbrBufferedReader(newList<String>lineList=newStringline=while((line=br.readLine())!=null)}returnlineList.toArray(newString[]}publicvoid{tryFileOutputStreamsrcnewStringlines"Java""C++""嵌入式""iOS""PHP",".NET""軟件測試""UID"IOUtils.saveLines(lines,src);}catch(IOException{}}publicvoid{tryFileInputStreamsrcnewString[]lines=}catch(IOException{}}:控制臺輸出[Java,C++,嵌入式,Android,iOS,PHP,.NET,軟件測試,擴(kuò)publicstaticvoidsaveLines(String[]lines,OutputStreamStringcharset)throwsIOExceptionBufferedWriterbw=newBufferedWriter(newOutputStreamWriter(src,if(lines!=null&&lines.length>0)for(inti=0;i<lines.length-1;}}bw.write(lines[lines.length-在Iils類中添加重載的aeines方法。構(gòu)造BuffeedipublicstaticvoidsaveLines(String[]lines,OutputStreamStringcharset)throwsIOExceptionBufferedWriterbw=newBufferedWriter(newOutputStreamWriter(src,if(lines!=null&&lines.length>0)for(inti=0;i<lines.length-1;}}bw.write(lines[lines.length- **@paramlines字符*@paramsrc*@paramcharset*@throws在Iil類中添加重載的ladine方法。構(gòu)造Buffeedeade類的對象,該類有eadine方法實現(xiàn)一個文本行,如果已到達(dá)流末尾,則返回null。在構(gòu)造Buffeedeade對象時,使用到Inputeameade,Inpueameade是字符輸出流通向字節(jié)輸出流的橋梁,使用InpuSeamReade(InpuSeami,Singhaet)構(gòu)造方法,來構(gòu)造Inputeameade類的對象。從基本輸入流中按照指定的字符集成 @paramsrc@paramcharset*@return從輸入流 *@throwspublicstaticString[]loadLines(InputStreamsrc,Stringcharset)throwsIOException{BufferedReaderbr=newnewInputStreamReader(src,List<String>lineList=newStringline=while((line=br.readLine())!={}returnlineList.toArray(newString[]}publicvoid{tryFileOutputStreamsrcnewString[]lines={"Java","C++","嵌入式","iOS""PHP",".NET""軟件測試""UID"IOUtils.saveLines(lines,src,"utf-}catch(IOException{}}publicvoid{tryFileInputStreamsrcnewString[]lines=IOUtils.loadLines(src,"utf-}catch(IOException{}}String[]lines=IOUtils.loadLines(src,"utf-8");中的“utf-8”改成“gbk會出現(xiàn)中亂碼現(xiàn)象,因此讀寫的編碼要控制臺輸出結(jié)果[Java,C++,宓屽叆寮?,Android,iOS,PHP,.NET,杞歡嫻嬭瘯,IOUtils類完整代碼如下所示publicclassIOUtils*把字節(jié)數(shù)組中數(shù) 到文件*@param *@parambs*@throwspublicstaticvoidsaveBytes(Filesrc,byte[]bs)throwsIOExceptionFileOutputStreamfos=tryfos=new}finallyif(fos!= *以字節(jié)的方 *@paramsrc * *@throwspublicstaticbyte[]loadBytes(Filesrc)throwsIOExceptionifthrownewIllegalArgumentException("源文件srcFileInputStreamfis=tryfis=newbyte[]bs=newreturn}finallyif(fis!=}}*使用緩沖字節(jié)流實現(xiàn)*@paramsrc*@paramdest*@throwspublicstaticvoidcopyFile(Filesrc,Filedest)throwsIOExceptionif(!src.exists())thrownewIllegalArgumentException("源文件src BufferedInputStreambis=BufferedOutputStreambos=trybis=newBufferedInputStream(newbos=newBufferedOutputStream(newintb=-while((b=-{}}finallytryif(bis!=if(bos!=}catch(Exceptione)}}}*使用字節(jié)流實現(xiàn)拷貝功*@paramis*@paramos*@throwspublicstaticvoidcopy(InputStreamis,OutputStreamos)throws{byte[]buffer=newbyte[8*intb=-while((b=is.read(buffer))!=-1)os.write(buffer,0,}} *增強(qiáng)OutputStream寫出double數(shù)*@paramdatadouble*@paramdest*@throwspublicstaticvoidsaveDouble(doubledata,OutputStreamthrowsIOExceptionDataOutputStreamdos=new *增強(qiáng) *@paramsrc輸入*@return從輸入流 的double數(shù)*@throwspublicstaticdoubleloadDouble(InputStreamsrc)throwsIOExceptionDataInputStreamdis=newdoubled=return *增強(qiáng)OutputStream寫出int數(shù)*@paramdataint*@paramdest*@throwspublicstaticvoidsaveInt(intdata,OutputStreamdest)throws DataOutputStreamdos=new *增強(qiáng) *@paramsrc基本輸入*@return*@throwspublicstaticintloadInt(InputStreamsrc)throwsIOExceptionDataInputStreamdis=newintd=return}*將字符串?dāng)?shù)組中的每一項內(nèi)容按行寫*@paramlines*@paramsrc*@throwspublicstaticvoidsaveLines(String[]lines,OutputStreamthrowsIOExceptionBufferedWriterbw=newBufferedWriter(newif(lines!=null&&lines.length>0)for(inti=0;i<lines.length-1;i++) bw.write(lines[lines.length- *從基本輸入流中成 文本存入字節(jié)數(shù)組返*@paramsrc基本輸入*@return從輸入流 *@throwspublicstaticString[]loadLines(InputStreamsrc)throwsIOExceptionBufferedReaderbr=newBufferedReader(newList<String>lineList=newStringline=while((line=br.readLine())!=null) returnlineList.toArray(newString[] 將字符串?dāng)?shù)組中的每一項內(nèi)容按行,按照指定的字符集寫入*@paramlines字符串?dāng)?shù)*@paramsrc基本輸出*@paramcharset字符*@throwspublicstaticvoidsaveLines(String[]lines,OutputStreamStringcharset)throwsIOExceptionBufferedWriterbw=newBufferedWriter(newif(lines!=null&&lines.length>0)for(inti=0;i<lines.length-1;i++) bw.write(lines[lines.length- *@paramsrc基本輸入*@paramcharset字符*@return從輸入流 *@throwspublicstaticString[]loadLines(InputStreamsrc,StringthrowsIOExceptionBufferedReaderbr=newBufferedReader(newList<String>lineList=newStringline=while((line=br.readLine())!=null) returnlineList.toArray(newString[] publicclassTestIOUtilspublicvoidtestSaveBytes()trybyte[]bs={1,2,3,4,5IOUtils.saveBytes(newFile("d:/temp/bytes.dat"),}catch(IOExceptione) publicvoidtestLoadBytes()trybyte[]bs=IOUtils.loadBytes(new}catch(IOExceptione) publicvoidtestCopy()tryIOUtils.copyFile(newFile("d:/temp/names.txt"),new}catch(IOExceptione) publicvoidtestCopyFile()tryIOUtils.copy(newnew}catch(IOExceptione) publicvoidtestSaveDouble()trydoubled=FileOutputStreamfos=newIOUtils.saveDouble(d,}catch(IOExceptione) publicvoidtestLoadDouble()tryFileInputStreamfos=newdoubled=}catch(IOExceptione) publicvoidtestSaveInt()tryintd=FileOutputStreamfos=newIOUtils.saveInt(d,}catch(IOExceptione) publicvoidtestLoadInt()tryFileInputStreamfos=newintd=}catch(IOExceptione) publicvoidtestSaveLines()tryFileOutputStreamsrc=newString[]lines"Java","C++","嵌入式"Android","iOS".NET"UIDIOUtils.saveLines(lines,}catch(IOExceptione) publicvoidtestLoadLines()tryFileInputStreamsrc=newString[]lines=}catch(IOExceptione) publicvoidtestSaveLinesWithCharset()tryFileOutputStreamsrc=newString[]lines"Java","C++","嵌入式"Android","iOS".NET"UIDIOUtils.saveLines(lines,src,"utf-}catch(IOExceptione) publicvoidtestLoadLinesWithCharset()tryFileInputStreamsrc=newString[]lines=IOUtils.loadLines(src,"utf-}catch(IOExceptione)}}}DMSUtils——完成saveLogDataList方法和loadLogDataList問在DMS.項目中,如何將LgDaa類的對象的集合到文件;如何從文件中數(shù)據(jù),形成LgDaa類的對象,并將LogDaa類的對象在集合中返回。方新建DMSUtils類,在類中添加saveLogDataList(List<LogDatalogDataList,FilelogFile),其中參數(shù)publicstaticvoidList<LogData>logDataList,File使用Pintie向文本輸出流ileupuSeam打印LogDaa對象的字符串形式,如:“ws,,, ,...”。pw=newPrintWriter(newfor(LogDatald:{}publicstaticList<LogData>loadLogDataList(FilelogFile)構(gòu)造Buffeedeade對象,從文件lgile中數(shù)據(jù),形成LgDaa類的對象,并將LgDaa類的對象在lgDaai集合中返回。br=newBufferedReader(newInputStreamReader(newStringline=while((line=br.readLine())!={LogDataloginLog=new}實系統(tǒng)代碼實現(xiàn)如*將List集合 *@paramlogRecList *@param publicstaticvoidsaveLogDataList(List<LogData>logDataList,File PrintWriterpw=trypw=newPrintWriter(newfor(LogDatald:logDataList) }catch(IOExceptione)thrownewRuntimeException("",}finallyif(pw!= *從文件 到List集合中返*@paramlogFile *@return publicstaticList<LogData>loadLogDataList(FilelogFile)BufferedReaderbr=List<LogData>logDataList=newtrybr=newBufferedReader(newInputStreamReader(new(br!=try}catch(IOExceptione)}}}Stringline=while((line=br.readLine())!=null){LogDataloginLog=newLogData(line);}return}catch(IOExceptione){excetin(",}finally在DMS.項目中,如何將Lgec類的對象的集合到文件;如何從文件中數(shù)據(jù),形成Lgec類的對象,并將Lgec類的對象到集合中返回。在DMSUtils類中添加saveLogRecList(List<LogRec>logRecList,FilelogRecFile),其中參數(shù)logRecList表示publicstaticvoidsaveLogRecList(List<LogRec>File @paramlogRecList *@parampublicstaticvoidsaveLogRecList(List<LogRec>logRecList,File PrintWriterpw=trypw=newPrintWriter(newfor(LogRecrec:logRecList)}catch(IOExceptione)thrownewRuntimeException("",}finallyif(pw!= } 在DMUil類中添加loadLgei方法,在方法中構(gòu)造Buffeedeade對象,從文件lgeile中數(shù)據(jù),形成Lge類的對象,并將LgRe類的對象在 *從文件 *@paramlogRecFile *@return publicstaticList<LogRec>loadLogRecList(FilelogRecFile)BufferedReaderbr=List<LogRec>logRecList=newtryb
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 房屋貼皮維修工人和工頭的合同(2篇)
- 二零二五年度男方房產(chǎn)贈與女方及子女的財產(chǎn)贈與合同14篇
- 二零二五年度離婚后子女撫養(yǎng)費(fèi)及探望權(quán)執(zhí)行合同4篇
- 2025年度智能農(nóng)貿(mào)場裝修升級合同4篇
- 二零二五年度農(nóng)藥行業(yè)供應(yīng)鏈金融服務(wù)合同4篇
- 二零二五版模具維修改型及知識產(chǎn)權(quán)保護(hù)合同3篇
- 二零二五版金融控股公司首席風(fēng)險官(CRO)合同3篇
- 2025年度寧波高端住宅項目購房合同范本4篇
- 2025年度特色民宿搭棚建設(shè)合同4篇
- 2025年度外墻涂料專業(yè)承包及售后服務(wù)合同4篇
- 長亭送別完整版本
- 2024年英語高考全國各地完形填空試題及解析
- 智能養(yǎng)老院視頻監(jiān)控技術(shù)方案
- 你比我猜題庫課件
- 無人駕駛航空器安全操作理論復(fù)習(xí)測試附答案
- 建筑工地春節(jié)留守人員安全技術(shù)交底
- 默納克-NICE1000技術(shù)交流-V1.0
- 蝴蝶蘭的簡介
- 老年人心理健康量表(含評分)
- 《小兒靜脈輸液速度》課件
- 營銷人員薪酬標(biāo)準(zhǔn)及績效考核辦法
評論
0/150
提交評論