Java IO学习笔记(四)打印流
生活随笔
收集整理的這篇文章主要介紹了
Java IO学习笔记(四)打印流
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、只有輸出流才有打印流:PrintWriter和PrintStream分別針對字符和字節,提供了重載的print,Println方法用于多種數據類型的輸出。PrintWriter和PrintStream操作不會拋出異常,數據沒打印出來也不會拋異常。 2、System.out.print(Object object)調用的是Object實體類的toString()方法。 3、PrintWriter和PrintStream有自動的flush功能,不需要手動調用flush方法。*這里必須強調一點:PrintWriter的自動flush功能,只有在new?PrintWriter對象的時候調用PrintWriter(Writer out, boolean autoFlush),并將 autoFlush設置為true才會自動flush,否則不會自動flush,必須手動printWriter.flush()才能成功打印數據。 ? ? ?另外,PrintWriter方法不僅可以套在Writer字符輸出流的外面,還可以套在字節輸出流外面,PrintWriter的構造方法如下: PrintStream的具體構造方法如下圖: 4、System.out.println()中,java.lang.System.out中的out就是PrintStream類型的。 ? 練習小程序1: package test.io.print; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; public class PrintStreamTest { ?????public static void main(String args[]){ ???????????PrintStream ps = null; ???????????FileOutputStream fos = null; ???????????try { ????????????????fos = new FileOutputStream("E:/技術學習/java/test/Socket/test6.txt"); ????????????????ps = new PrintStream(fos); ????????????????System.setOut(ps);//不再是控制臺輸出,而是輸出到FileOutputStream所指向的文件中。 ????????????????for(int i=0; i<3000; i++){ ?????????????????????System.out.print(i+" "); ????????????????} ???????????} catch (FileNotFoundException e) { ????????????????e.printStackTrace(); ???????????} finally{ ????????????????try { ?????????????????????if(ps != null){ ???????????????????????????ps.close(); ?????????????????????} ?????????????????????if(fos != null){ ???????????????????????????fos.close(); ?????????????????????} ????????????????}catch (IOException e){ ?????????????????????e.printStackTrace(); ????????????????} ???????????}???? ?????} } ? 練習小程序2:從控制臺輸入內容,并將控制臺輸入的內容打印到文件中,類似于記日志 package test.io.print; import java.io.BufferedReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Date; ? public class PrintWriterTest { ? ? public static void main(String args[]){ ? ? ? ? BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); ? ? ? ? String s = null; ? ? ? ? PrintWriter pw = null; ? ? ? ? try { ? ? ? ? ? ? //PrintWriter構造方法將是否自動flush設置成了true,即可自動flush,如果沒設置true,則必須手動調用flush方法,才能把內容打印到文件中 ? ? ? ? ? ? pw = new PrintWriter(new FileWriter("E:/技術學習/java/test/Socket/test7.txt",true),true); ? ? ? ? ? ? while((s=bf.readLine()) != null){ ? ? ? ? ? ? ? ? if(s.equals("exit")){ ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? System.out.println(s); ? ? ? ? ? ? ? ? pw.println("---------------------------------------"); ? ? ? ? ? ? ? ? pw.println(s); ? ? ? ? ? ? ? ? //pw.flush(); ? ? ? ? ? ? } ? ? ? ? ? ? pw.println("-------------"+new Date()+"------------"); ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } finally{ ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? if(bf != null){ ? ? ? ? ? ? ? ? ? ? bf.close(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if(pw != null){ ? ? ? ? ? ? ? ? ? ? pw.close(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? } ? ? ? ? } ? ? } }
轉載于:https://www.cnblogs.com/bjh1117/p/6386941.html
總結
以上是生活随笔為你收集整理的Java IO学习笔记(四)打印流的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP实现支付宝即时到账功能
- 下一篇: Android 聊天软件客户端