java Print流 和 重定向

1、Print流 分爲PrintStream和 PrintWriter 兩種

 他們表示的含義可以從他們的構造函數中看出:

 PrintStream extends FilterOutputStream:

 private PrintStream(boolean autoFlush, OutputStream out, Charset charset) {

        super(out);

        this.autoFlush = autoFlush;

        this.charOut = new OutputStreamWriter(this, charset);

        this.textOut = new BufferedWriter(charOut);

    }

    首先它是一個OutputStream的子類

    其次它是一個包裹流,在其輸出字節流的基礎上包裹了轉換流,轉換成字符輸出流,然後又包裹了一層緩衝輸出流

    最後他的flush功能默認是關閉的,所以需要進行開啓或者手動flush.

    PrintWriter extends Writer:

    public PrintWriter(OutputStream out, boolean autoFlush) {

        this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);

        // save print stream for error propagation

        if (out instanceof java.io.PrintStream) {

            psOut = (PrintStream) out;

        }

    }

    可以看出PrintWriter對於字節流的處理和PrintStream相同,不過它也可以用Writer流作爲參數。

    public PrintWriter(Writer out,boolean autoFlush) {

        super(out);

        this.out = out;

        this.autoFlush = autoFlush;

        lineSeparator = java.security.AccessController.doPrivileged(

            new sun.security.action.GetPropertyAction("line.separator"));

    }

    

2、關於輸出流所提供的功能(方法)對比

    - Writer :// FileWriter\CharArrayWriter

      write(int) 一個字符

      write(char[]) 一個字符數組

      write(char[],offset,len) 一個字符數組的一部分

      write(string,offset,len) 一個字符串或者它的一部分

      

    - OutputStream

      write(int) 一個字節

      write(byte[]) 一個字節數組

      write(byte[],offset,len) 一個字節數組的一部分

      

    - DataOutputStream

      在一個OutputStream的基礎上提供以下功能

      writeInt(int) 把一個int型數據按照字節輸出。100 --> 0x 00 00 00 64

      writeLong(long) 把一個long型數據按照字節輸出。 

    

    - PrintStream 

      首先它繼承自OutputStream,是一個字節流

      其次它以字符形式進行輸出。也就是說它會自動按照指定的編碼把字節中的數據轉換爲字符,然後進行輸出。集合了轉換和緩衝輸出的功能,參考上面的構造函數

      最後它主要是提供了許多格式化輸出功能

      write(int) 字節輸出 100 --> 64

      write(byte[],offset,len) // 字節輸出

      write(char[]) 字符輸出

      write(string) BufferedWriter 字符輸出

      print(...) 字符輸出 100 --> 100

      println(...) 字符輸出 '\r\n'

      printf(format,...) 字符輸出

      

    - PrintWriter

      它的方法和PrintStream相同。

      關於換行符,在windows下是\r\n, 而在linux下是\n,

      他們的用法基本相同

      public class TestIO {

 public static void main(String[] args) {

  String filename = "D:" + File.separator + "hello.txt";

  

  File file = new File(filename);

  

  

  try {

   FileOutputStream fout = new FileOutputStream(file);

   

   PrintStream prt = new PrintStream(fout);

   

   prt.write(100); // 文件中內容爲64

   

   prt.close();

   fout.close();

   

   

  } catch (FileNotFoundException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  

 }

}        

 

Print 流通常用於標準輸出和標準錯誤,他們默認都是在顯示器上進行輸出。我們可以更改他們輸出到文件,這就是輸出重定向,或者叫做標準輸出重定向

   System.out 標準輸出流

   System.err 標準錯誤流

        

   System.setErr()

   System.setOut()

  

?Test Code:

 public class TestIO {

 public static void main(String[] args) {

  String filename = "D:" + File.separator + "hello.txt";

  File file = new File(filename);

  

  System.out.println("標準輸出 -- 控制檯");

  

  try {

   //FileOutputStream fileOut = new FileOutputStream(filename);

   PrintStream prt = new PrintStream(file);

   

   System.setOut(prt);

   

   System.out.println("標準輸出重定向 -- 文件");

   

   

  } catch (FileNotFoundException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

 

 }

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章