Java中PrintStream(列印輸出資料流),javaprintstream

來源:互聯網
上載者:User

Java中PrintStream(列印輸出資料流),javaprintstream
Java中PrintStream(列印輸出資料流) PrintStream 是列印輸出資料流,它繼承於FilterOutputStream。PrintStream 是用來裝飾其它輸出資料流。它能為其他輸出資料流添加了功能,使它們能夠方便地列印各種資料值表示形式。與其他輸出資料流不同,PrintStream 永遠不會拋出 IOException;它產生的IOException會被自身的函數所捕獲並設定錯誤標記, 使用者可以通過 checkError() 返回錯誤標記,從而查看PrintStream內部是否產生了IOException。另外,PrintStream 提供了自動flush 和 字元集設定功能。所謂自動flush,就是往PrintStream寫入的資料會立刻調用flush()函數。




常用轉換 FileReader——>BufferedReaderBufferedReader in= new BufferedReader(new FileReader("Text.java")); InputStream——>InputStreamReader——>BufferedReaderBufferedReader in=new BufferedReader(new InputStreamReader(System.in)); String——>byte[]——>ByteArrayInputStream——>DataInputStreamDataInputStream in= new DataInputStream(new ByteArrayInputStream(str.getBytes())); FileInputStream——>BufferedInputStream——>DataInputStreamDataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream("Data.txt"))); FileWriter——>BufferedWriter——>PrintWriterPrintWriter pw=new PrintWriter(new BufferedWriter("text.out")); System.out(PrintStream)——>PrintWriterPrintWriter pw=new PrintWriter(System.out,true); FileOutputStream——>BufferedOutputStream——>PrintStreamPrintStream ps= new PrintStream(new BufferedOutputStream(new FileOutputStream("text.out"))); FileOutputStream——>BufferedOutputStream——>DataOutputStreamDataOutputStream dos= new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Data.txt")));  構造方法:PrintStream(File file)  建立具有指定檔案且不帶自動行重新整理的新列印流。PrintStream(File file, String csn) 建立具有指定檔案名稱和字元集且不帶自動行重新整理的新列印流。PrintStream(OutputStream out)   建立新的列印流。PrintStream(OutputStream out, boolean autoFlush)    建立新的列印流。PrintStream(OutputStream out, boolean autoFlush, String encoding)      建立新的列印流。PrintStream(String fileName)        建立具有指定檔案名稱且不帶自動行重新整理的新列印流。PrintStream(String fileName, String csn)  建立具有指定檔案名稱和字元集且不帶自動行重新整理的新列印流。 PrintStream和DataOutputStream異同點相同點:都是繼承與FileOutputStream,用於封裝其它輸出資料流。不同點:(01) PrintStream和DataOutputStream 都可以將資料格式化輸出;但它們在“輸出字串”時的編碼不同。     PrintStream是輸出時採用的是使用者指定的編碼(建立PrintStream時指定的),若沒有指定,則採用系統預設的字元編碼。而DataOutputStream則採用的是UTF-8。       關於UTF-8的字元編碼可以參考“字元編碼(ASCII,Unicode和UTF-8) 和 大小端”      關於DataOutputStream的更多內容,可以參考“java io系列15之 DataOutputStream(資料輸出資料流)的認知、源碼和樣本”(02) 它們的寫入資料時的異常處理機制不同。       DataOutputStream在通過write()向“輸出資料流”中寫入資料時,若產生IOException,會拋出。       而PrintStream在通過write()向“輸出資料流”中寫入資料時,若產生IOException,則會在write()中進行捕獲處理;並設定trouble標記(用於表示產生了異常)為true。使用者可以通過checkError()返回trouble值,從而檢查輸出資料流中是否產生了異常。(03) 建構函式不同       DataOutputStream的建構函式只有一個:DataOutputStream(OutputStream out)。即它只支援以輸出資料流out作為“DataOutputStream的輸出資料流”。       而PrintStream的建構函式有許多:和DataOutputStream一樣,支援以輸出資料流out作為“PrintStream輸出資料流”的建構函式;還支援以“File對象”或者“String類型的檔案名稱對象”的建構函式。       而且,在PrintStream的建構函式中,能“指定字元集”和“是否支援自動flush()操作”。 (04) 目的不同       DataOutputStream的作用是裝飾其它的輸出資料流,它和DataInputStream配合使用:允許應用程式以與機器無關的方式從底層輸入資料流中讀寫java資料類型。       而PrintStream的作用雖然也是裝飾其他輸出資料流,但是它的目的不是以與機器無關的方式從底層讀寫java資料類型;而是為其它輸出資料流提供列印各種資料值表示形式,使其它輸出資料流能方便的通過print(), println()或printf()等輸出各種格式的資料。  http://www.apihome.cn/api/java/PrintStream.htmlhttp://www.cnblogs.com/skywang12345/p/io_16.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.