【JAVA IO】_System類對IO的支援筆記

來源:互聯網
上載者:User

【JAVA IO】_System類對IO的支援筆記

本章目標:
掌握System對IO的三種支援:
System.out
System.err
System.in
掌握System.out及System.err的區別
掌握輸入、輸出重新導向

System類的常量

System表示系統類別,此類在之前講解java常用類庫的時候就已經為讀者介紹過了,實際上在java中System類也對IO給予了一定的支援。
No.    System類的常量                描述
1    public static final PrintStream out    對應系統標準輸出,一般是顯示器
2    public static final PrintStream err    錯誤資訊輸出
3    public static final InputStream in    對應著標準輸入,一般是鍵盤

3.1、System.out

使用System.out輸出的時候就是將輸出的位置定義在了顯示器之中。
FileOutputStream是定位在檔案裡,而System.out是定位在螢幕上。
使用OutputStream完成螢幕上輸出。

PrintStream就是OutputStream的子類

下面的執行個體真正體現了啥叫流的概念。

import java.io.OutputStream;import java.io.IOException;public class SystemDemo01{    public static void main(String args[]){        OutputStream out = System.out;    //此時的輸出資料流是向螢幕上輸出        try{            out.write("hello world!!!".getBytes());        }catch(IOException e){            e.printStackTrace();        }        try{            out.close();        }catch(IOException e){            e.printStackTrace();        }    }}

很明顯就是對象多態性的體現,根據實現的子類不同,完成的功能也不同。

3.2、System.err
System.err表示的是錯誤的標準輸出,如果程式中出現了錯誤的話,則直接使用System.err進行列印輸出即可。

public class SystemDemo02{    public static void main(String[] args){        String str = "hello";        try{            System.out.println(Integer.parseInt(str));    //轉型        }catch(Exception e){            System.orr.println(e);        }    }}

錯誤資訊:java.lang.NumberFormatException:For input string:"hello"

System.out和System.err的區別:

System.out和System.err都是PrintStream的執行個體化對象,而且通過執行個體代碼可以發現,兩者都可以輸出錯誤資訊,但是一般來講System.out是將資訊顯示給使用者看,是正常的資訊顯示,而System.err的資訊正好相反是不希望使用者看到的,會直接在後台列印,是專門顯示錯誤的。

一般來講輸出錯誤資訊時最好使用System.err.println()    

3.3、System.in
System.in實際上是一個鍵盤的輸入資料流,其本身是InputStream類型的對象。那麼,此時可以利用此方式完成從鍵盤讀取資料的功能。

import java.io.InputStream;public class SystemDemo04{    public static void main(String args[])throws Exception{        InputStream input = System.in;        byte b[] = new byte[1024];        System.out.println("請輸入內容");        int len = input.read(b);        System.out.println("輸入的內容為:"+new String(b,0,len));        input.close();    }}

以上程式的問題:

問題一:指定了輸入資料的長度,如果現在輸入的資料超過了長度範圍,則只能輸入部分的資料。
問題二:指定的byte數組長度是奇數的話,則還有可能出現中文亂碼問題。

只能通過判斷標誌位的方式完成。

import java.io.InputStream ;public class SystemDemo05{    public static void main(String args[]) throws Exception {    // 所有異常拋出        InputStream input = System.in ;    // 從鍵盤接收資料        StringBuffer buf = new StringBuffer() ;    // 使用StringBuffer接收資料        System.out.print("請輸入內容:") ;    // 提示資訊        int temp = 0 ;        // 接收內容        while((temp=input.read())!=-1){            char c = (char) temp ;    // 將資料變為字元            if(c=='\n'){    // 退出迴圈,輸入斷行符號表示輸入完成                break ;            }            buf.append(c) ;    // 儲存內容        }        System.out.println("輸入的內容為:" + buf) ;        input.close() ;    // 關閉輸入資料流    }};

此時,沒有長度限制,願意輸入多少就輸入多少,但是真的可以執行嗎?
如果現在輸入的是中文呢?

最好的輸入方式:
最好的輸入方式是將全部輸入的資料暫時放到一塊記憶體之中,之後一次性的從記憶體中讀取出資料,這樣所有的資料唯讀了一次,則不會造成亂碼,而且也不會受長度的限制。
要想實現以上的功能只能通過BufferedReader類完成。

3.4、輸入、輸出重新導向。

輸入、輸出重新導向可以通過以下方法:

No.    方法或常量                    描述
1    public static void setOut(PrintStream out)    重新導向“標準”輸出資料流
2    public static void setErr(PrintStream err)                錯誤輸出資料流
3    public static void setIn(PrintStream in)            輸入資料流

import java.io.File ;import java.io.FileOutputStream ;import java.io.PrintStream ;public class SystemDemo06{    public static void main(String args[]) throws Exception {        System.setOut(            new PrintStream(                new FileOutputStream("d:" +                    File.separator + "red.txt"))) ;    // System.out輸出重新導向        System.out.print("www.mldnjava.cn") ;    // 輸出時,不再向螢幕上輸出        System.out.println(",李興華") ;    }};
import java.io.File ;import java.io.FileOutputStream ;import java.io.PrintStream ;public class SystemDemo07{    public static void main(String args[]){        String str = "hello" ;        // 聲明一個非數位字串        try{            System.out.println(Integer.parseInt(str)) ;    // 轉型        }catch(Exception e){            try{                System.setOut(                    new PrintStream(                        new FileOutputStream("d:"                            + File.separator + "err.log"))) ;    // 輸出重新導向            }catch(Exception e1){                        }            System.out.println(e) ;        }    }};

import java.io.ByteArrayOutputStream ;import java.io.PrintStream ;public class SystemDemo08{    public static void main(String args[]) throws Exception{    // 所有異常拋出        ByteArrayOutputStream bos = null ;        // 聲明記憶體輸出資料流        bos = new ByteArrayOutputStream() ;        // 執行個體化        System.setErr(new PrintStream(bos)) ;    // 輸出重新導向        System.err.print("www.mldnjava.cn") ;    // 錯誤輸出,不再向螢幕上輸出        System.err.println("李興華") ;            // 向記憶體中輸出        System.out.println(bos) ;    // 輸出記憶體中的資料    }};
import java.io.FileInputStream ;import java.io.InputStream ;import java.io.File ;public class SystemDemo09{    public static void main(String args[]) throws Exception{    // 所有異常拋出        System.setIn(new FileInputStream("d:"            + File.separator + "demo.txt")) ;    // 設定輸入重新導向        InputStream input = System.in ;    // 從檔案中接收資料        byte b[] = new byte[1024]    ;// 開闢空間,接收資料        int len = input.read(b) ;    //接收        System.out.println("輸入的內容為:" + new String(b,0,len)) ;        input.close() ;    // 關閉輸入資料流    }};

聯繫我們

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