- /** ?
- ?????*?輸入流的簡單測試 ?
- ?????*?@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()];??
- ???????? //讀取到第幾個byte ??
- ???????? int ?count= 0 ;??
- ???????? //每次讀取一個字節,若返回-1則表示讀完了 ??
- ???????? while ((i=ins.read())!=- 1 ){??
- ???????????? //將讀到的一個byte數字放入數組中 ??
- ????????????countByte[count]=( byte )i;??
- ????????????count++;??
- ????????}??
- ???????? //將byte數組轉換為字符串 ??
- ????????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;??
- ????}??
文件輸出流的簡單測試及文件復制的實現
?
- /** ?
- ?????*?文件輸入輸出流的簡單測試實現文件的復制 ?
- ?????*?@param?srcName:原文件 ?
- ?????*?@param?bakName:拷貝后的文件 ?
- ?????*?@return:復制是否成功的真假 ?
- ?????*?@throws?IOException ?
- ?????*/ ??
- ???? public ? boolean ?copyFile(String?srcName,String?bakName) throws ?IOException{??
- ???????? //創建從源文件來的輸入流 ??
- ????????InputStream?ins= new ?FileInputStream(srcName);??
- ???????? //輸出InputStream流對象,若文件中已有內容則覆蓋原來的內容 ??
- ????????OutputStream?ous= new ?FileOutputStream(bakName);??
- ???????? int ?i= 0 ;??
- ???????? //從輸入流中讀取一個字節 ??
- ???????? while ((i=ins.read())!=- 1 ){??
- ???????????? //將這個字節寫入到輸出流 ??
- ????????????ous.write(i);??
- ????????}??
- ????????ins.close();??
- ???????? //清空輸出流的緩存并關閉 ??
- ????????ous.flush();??
- ????????ous.close();??
- ???????? return ? true ;??????
- ????}??
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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