- /** ?
- ?????*?輸入流的簡(jiǎn)單測(cè)試 ?
- ?????*?@param?fileName:文件名 ?
- ?????*?@return:讀入的字符串 ?
- ?????*?@throws?java.io.IOException ?
- ?????*/ ??
- ???? public ?String?readFileOne(String?fileName)? throws ?java.io.IOException{??
- ????????InputStream?ins= new ?FileInputStream(fileName);??
- ???????? int ?i=- 1 ;??
- ???????? byte []?countByte= new ? byte [ins.available()];??
- ???????? //讀取到第幾個(gè)byte ??
- ???????? int ?count= 0 ;??
- ???????? //每次讀取一個(gè)字節(jié),若返回-1則表示讀完了 ??
- ???????? while ((i=ins.read())!=- 1 ){??
- ???????????? //將讀到的一個(gè)byte數(shù)字放入數(shù)組中 ??
- ????????????countByte[count]=( byte )i;??
- ????????????count++;??
- ????????}??
- ???????? //將byte數(shù)組轉(zhuǎn)換為字符串 ??
- ????????String?s= new ?String(countByte);??
- //????????????????byte[]?getBytes=new?byte[50]; ??
- //??????int?state=ins.read(getBytes); ??
- //??????while(state!=-1){ ??
- //???????????????for(int?i=0;i<getBytes.length;i++){ ??
- //??????????System.out.print((char)getBytes[i]); ??
- //??????????} ??
- //??????????????state=ins.read(getBytes); ??
- //??????????} ??
- ????????ins.close();??
- ???????? return ?s;??
- ????}??
文件輸出流的簡(jiǎn)單測(cè)試及文件復(fù)制的實(shí)現(xiàn)
?
- /** ?
- ?????*?文件輸入輸出流的簡(jiǎn)單測(cè)試實(shí)現(xiàn)文件的復(fù)制 ?
- ?????*?@param?srcName:原文件 ?
- ?????*?@param?bakName:拷貝后的文件 ?
- ?????*?@return:復(fù)制是否成功的真假 ?
- ?????*?@throws?IOException ?
- ?????*/ ??
- ???? public ? boolean ?copyFile(String?srcName,String?bakName) throws ?IOException{??
- ???????? //創(chuàng)建從源文件來(lái)的輸入流 ??
- ????????InputStream?ins= new ?FileInputStream(srcName);??
- ???????? //輸出InputStream流對(duì)象,若文件中已有內(nèi)容則覆蓋原來(lái)的內(nèi)容 ??
- ????????OutputStream?ous= new ?FileOutputStream(bakName);??
- ???????? int ?i= 0 ;??
- ???????? //從輸入流中讀取一個(gè)字節(jié) ??
- ???????? while ((i=ins.read())!=- 1 ){??
- ???????????? //將這個(gè)字節(jié)寫(xiě)入到輸出流 ??
- ????????????ous.write(i);??
- ????????}??
- ????????ins.close();??
- ???????? //清空輸出流的緩存并關(guān)閉 ??
- ????????ous.flush();??
- ????????ous.close();??
- ???????? return ? true ;??????
- ????}??
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
