Java IO學習筆記(四)列印流

來源:互聯網
上載者:User

標籤:file   amt   ade   put   span   ati   pack   rgs   練習   

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();            }        }    }}

Java IO學習筆記(四)列印流

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.