日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 芯聞號(hào) > 充電吧
[導(dǎo)讀]1. IO流的異常處理方式 ? try 外聲明,try內(nèi)new 對(duì)象 ? 關(guān)閉動(dòng)作,必須寫在finally ? 防止空指針異常,進(jìn)行判斷 ? 如果開啟了多個(gè)流對(duì)象,分別進(jìn)行關(guān)閉,獨(dú)立寫try catc

1. IO流的異常處理方式 ? try 外聲明,try內(nèi)new 對(duì)象 ? 關(guān)閉動(dòng)作,必須寫在finally ? 防止空指針異常,進(jìn)行判斷 ? 如果開啟了多個(gè)流對(duì)象,分別進(jìn)行關(guān)閉,獨(dú)立寫try catch ? eclipse中,不寫路徑,直接寫文件名,文件保存在工程根目錄下 ? 追加寫入,不會(huì)覆蓋源文件,但是構(gòu)造方法中,寫true ? 換行,需要換行的位置rn r回車符 n 換行符
/* ? IO流中的異常處理 ? IO異常處理中。關(guān)閉資源close()方法,必須單獨(dú)寫try.. catch */ import java.io.*; class FileWriterDemo2 { ?public static void main(String[] args) ?{ ? //try外聲明變量,try內(nèi)建立對(duì)象 ? FileWriter fw = null; ? try{ ? fw = new FileWriter("Exception.txt");//fw = null ? fw.write("異常"); ? fw.flush(); ? fw.write("沒有異常"); ? fw.flush(); ? }catch(IOException e){ ? ? //將異常信息打印出來 ? ? e.printStackTrace(); ? ? //如果真的發(fā)生IO異常,必須將程序停止 ? ? ? ? ? throw new RuntimeException("文件寫入失敗"); ? }finally{ ? ? ?//執(zhí)行關(guān)閉流對(duì)象 ? ? ?try{ ? ? if(fw!=null)//說明對(duì)象建立成功,IO流開始使用Windows功能了 ? ? ? ? fw.close(); ? ? ?}catch(IOException e){ ? ? ? ? e.printStackTrace(); ? ? ?throw new RuntimeException("關(guān)閉資源失敗"); ? ? ?} ? } ?} } //=

2. 字符流復(fù)制文本文件 ? 將C:\r.html復(fù)制到D盤去 ? C盤上的文件,數(shù)據(jù)源,讀取的 ? D盤上的文件,數(shù)據(jù)目的,輸出的 ? 第一種,讀取一個(gè)字符,寫一個(gè)字符 難以忍受 ? 第二種,讀取一個(gè)數(shù)組,寫一個(gè)數(shù)組 最快 ? 第三種,讀取一行,寫一行,寫一個(gè)換行 其次
/* ?* 字節(jié)流復(fù)制任意文件,異常處理 ?* 讀寫單個(gè)字節(jié) ?*/ import java.io.*; public class CopyFile { ?public static void main(String[] args) { ? long s = System.currentTimeMillis(); ? FileInputStream fis = null; ? FileOutputStream fos = null; ? try{ ? ?fis = new FileInputStream("e:\apache.exe"); ? ?fos = new FileOutputStream("d:\apache.exe"); ? ?int len = 0 ; ? ?while((len = fis.read())!=-1){ ? ? fos.write(len); ? ?} ? }catch(IOException e){ ? ?e.printStackTrace(); ? ?throw new RuntimeException("文件復(fù)制失敗"); ? }finally{ ? ?try{ ? ? if(fos!=null) ? ? ?fos.close(); ? ?}catch(IOException e){ ? ? throw new RuntimeException("文件寫入關(guān)閉失敗"); ? ?}finally{ ? ? try{ ? ? ?if(fis!=null) ? ? ? fis.close(); ? ? }catch(IOException e){ ? ? ?throw new RuntimeException("文件讀取關(guān)閉失敗"); ? ? } ? ?} ? } ? long e = System.currentTimeMillis(); ? System.out.println(e-s); ?} } //============================== /* ?* 字節(jié)流復(fù)制文件,數(shù)組緩沖 ?*/ import java.io.*; public class CopyFile1 { ?public static void main(String[] args) { ? long s = System.currentTimeMillis(); ? FileInputStream fis = null; ? FileOutputStream fos = null; ? try{ ? ?fis = new FileInputStream("c:\apache.exe"); ? ?fos = new FileOutputStream("d:\apache.exe"); ? ?int len = 0 ; ? ?byte[] bytes = new byte[1024]; ? ?while((len = fis.read(bytes))!=-1){ ? ? fos.write(bytes,0,len); ? ?} ? }catch(IOException e){ ? ?e.printStackTrace(); ? ?throw new RuntimeException("文件復(fù)制失敗"); ? }finally{ ? ?try{ ? ? if(fos!=null) ? ? ?fos.close(); ? ?}catch(IOException e){ ? ? throw new RuntimeException("文件寫入關(guān)閉失敗"); ? ?}finally{ ? ? try{ ? ? ?if(fis!=null) ? ? ? fis.close(); ? ? }catch(IOException e){ ? ? ?throw new RuntimeException("文件讀取關(guān)閉失敗"); ? ? } ? ?} ? } ? long e = System.currentTimeMillis(); ? System.out.println(e-s); ?} } //============================== package cn.itcast.iostream; /* ?* 利用字節(jié)流的緩沖區(qū)對(duì)象復(fù)制 ?*/ import java.io.*; public class CopyFile2 { ?public static void main(String[] args) { ? long s = System.currentTimeMillis(); ? BufferedInputStream bis = null; ? BufferedOutputStream bos = null; ? try{ ? ?bis = new BufferedInputStream(new FileInputStream("c:\apache.exe")); ? ?bos = new BufferedOutputStream(new FileOutputStream("d:\apache.exe")); ? ?int len = 0 ; ? ?while((len = bis.read())!=-1){ ? ? bos.write(len); ? ?} ? }catch(IOException e){ ? ?e.printStackTrace(); ? ?throw new RuntimeException("復(fù)制失敗"); ? }finally{ ? ?try{ ? ? if(bos!=null) ? ? ?bos.close(); ? ?}catch(IOException e){ ? ? throw new RuntimeException("文件寫入關(guān)閉失敗"); ? ?}finally{ ? ? try{ ? ? ?if(bis!=null) ? ? ? bis.close(); ? ? }catch(IOException e){ ? ? ?throw new RuntimeException("文件讀取關(guān)閉失敗"); ? ? } ? ?} ? } ? long e = System.currentTimeMillis(); ? System.out.println(e-s); ?} }
?//

3. 字符流的緩存區(qū)對(duì)象
?字符流的緩存區(qū)對(duì)象 ?BufferedWriter ? ?提高字符流對(duì)象,輸出流的寫入效率 ? ?構(gòu)造方法 BufferedWriter(Writer out) 傳遞的是Writer子類 ? ?這個(gè)構(gòu)造方法中,可以傳遞一切字符輸出流 ? ?傳遞的字符輸出流,這個(gè)緩沖區(qū)就會(huì)提高我們傳遞的流對(duì)象的效率 ? ?FileWriter ? ?new BufferedWriter(FileWriter) ? ? ? ?特殊的方法 ? ?void newLine() 寫入一個(gè)新行 ? ?我寫的時(shí)候,rn同樣換行 ? ?為什么在緩沖區(qū)對(duì)象中,提供一個(gè)換行的方法呢 ? ?原因在于Windows Linux換行符號(hào)不一樣 ? ?Windows rn Linux n
? ?newLine()方法,具有跨平臺(tái)性 ? ?安裝的JDK是Windows版本的,newLine方法中,寫的就是rn ? ?安裝的JDK是Linux版本的, newLine方法中,寫的就是n ? ?記事本rn ? ?editplus rn n
?BufferedReader ? 提高字符輸入流對(duì)象,提高讀取效率 ? 構(gòu)造方法BufferedReader(Reader in) 傳遞一個(gè)字符輸入流對(duì)象 ? FileReader ? new BufferedReader(FileReader) ? 提供一個(gè)新的方法,讀一行 ? String readLine() 讀取一個(gè)新行,按行讀取 ? 每次讀取一行,不管一行有多長(zhǎng) ? 返回一行的內(nèi)容 ? 讀取到文件末尾,返回null; ? readLine()方法,讀取完以后之后,返回的有沒有換行符號(hào),沒有換行符號(hào) ? 只有有效字符,并沒有rn //==

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉