Java IO Stream 總結

Java IO Stream 總結

Stream 是在編程語言中對輸入輸出的總稱 (一種比喻的稱謂。Stream 爲流水,輸入輸出實際上就是指數據的流動,數據由一個地方運動的另外一個地方,就像流水同樣,程序員將輸入輸出比做流水,再恰當不過了。)
 
 
流按照其所載內容分類,大體可分爲字節流和字符流兩大類
 
字節流 Byte Stream
在計算機中,byte是至關於機器語言中的單詞,他在Java中統一由InputStreamOutputStream做處理。
 
字符流(Character Stream
而在編碼體系中,通常採用Char2 bytes, 他在Java中統一由ReaderWriter做處理。
 
InputStream, OutputStream, ReaderWriter, 做爲在java.io.*包的頂級父類,定義了IO Process中最抽象的處理和規範。對於實際的應用,他們並不適用。因而根據各類實際的須要,由他們派生出來形式各樣各具特點的子類。
 
下表概述了Java IO 經常使用Classes 的關係:
 

經常使用Java IO Classes 關係圖
Byte
InputStream/ OutputStream
Node
Byte Stream
FileInputstream/ FileOutputStream
 
PipeInputStream/
PipeOutputStream
 
 
Processing Byte Stream
FilterInputStream/
FilterOutputStream
BufferInputStream/
BufferOutputStream
DataInputStream/
DataOutputStream
PrintStream
 
 
Byte Char 經過 InputstreamReader OutputStreamWriter 來轉換
Char
Reader/Writer
Node
Char Stream
FileReader/FileWriter
 
PipeReader/PipeWriter
 
 
Processing Char Stream
 
BufferReader/
BufferWriter
PrintWriter
 
 

 
 

(一)Stream的分類:

1  Node Stream :基本流,能夠從名稱中看出他是從哪一個地方輸入輸出的。
1.1 用於文件輸入輸出流: FileInputStream, FileOutputStream
1.2 用於內存數組的輸入輸出流:ByteArrayInputStream, ByteArrayOutputStream
1.3 用於字符串的輸入輸出流:StringArrayInputStream, StringArrayOutputStream
1.4 用於管道的輸入輸出流:PipedInputStream, PipeOutStream (用於線程間的交互)
….
2  Processing Stream: 處理流,是對Node Stream的增強和補充,能夠看做是高級流。 要構造一個高級流一般要以一個基礎流爲基礎(如經過構造函數的參數傳入)
2.1 用於提升輸入輸出效率的緩衝流:BufferedInputStream, BufferedOutputStream
2.2 用於數據轉化的數據流: DataInputStream (用於讀取JavaPrimitive Data Type) , DataOutputStream
2.3 8位轉化爲16位的流: InputStreamReader, OutputWriter (用於溝通byte Char )
2.4 打印流: PintStream
….
 
(二)幾個重要的IO Classes
 
InputStream

abstract  int
(可對應Char)
read ()
          Reads the next byte of data from the input stream
int
read (byte[] b)
          Reads some number of bytes from the input stream and stores them into the buffer array b.
void
close ()
          Closes this input stream and releases any system resources associated with the stream. (Stream
用完以後要注意關閉!)

 
OutputStream

abstract  void
write (int b)
          Writes the specified byte to this output stream.
void
write (byte[] b)
          Writes b.length bytes from the specified byte array to this output stream.
void
close ()
          Closes this output stream and releases any system resources associated with this stream.
 void
flush ()
          Flushes this output stream and forces any buffered output bytes to be written out.
(沒必要等buffer滿了再寫出,強行把全部的東西都寫出來)

 
 
DataInputStream
可以讀出在輸入流中讀出Java的基本數據類型(primitive data type),常在對輸入流格式十分清楚的狀況下使用.
 

 boolean
 byte
 char
 double
 float
 int
readInt ()

 
 
DataOutputStream
可以直接寫出Java的基本數據類型
 

void
writeBoolean (boolean v)
          Writes a boolean to the underlying output stream as a 1-byte value.
 void
writeByte (int v)
          Writes out a byte to the underlying output stream as a 1-byte value.
void
writeDouble (double v)
          Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first.
 void
writeFloat (float v)
          Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first.
 void
writeInt (int v)
          Writes an int to the underlying output stream as four bytes, high byte first.

 
 
FileReader
 

Constructor Summary
FileReader ( File  file)
          Creates a new FileReader, given the File to read from.
 
FileReader ( String  fileName)
          Creates a new FileReader, given the name of the file to read from.
 

 
 
FileWriter
 

Constructor Summary
FileWriter ( File  file)
          Constructs a FileWriter object given a File object.
 
FileWriter ( File  file, boolean append)
          Constructs a FileWriter object given a File object.
 
FileWriter ( String  fileName)
          Constructs a FileWriter object given a file name.
 
FileWriter ( String  fileName, boolean append)
          Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.
 

 
 
PrintWriter 最好的Writer (提供了咱們熟悉的println()方法)
 

Constructor Summary
PrintWriter ( File  file)
          Creates a new PrintWriter, without automatic line flushing, with the specified file.
 
PrintWriter ( OutputStream  out)
          Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream.
 
PrintWriter ( Writer  out)
          Creates a new PrintWriter, without automatic line flushing.
 

 

 void
println (boolean x)
          Prints a boolean value and then terminates the line.
 void
println (char x)
          Prints a character and then terminates the line.
 void
println (double x)
          Prints a double-precision floating-point number and then terminates the line.
 void
println (float x)
          Prints a floating-point number and then terminates the line.
 void
println (long x)
          Prints a long integer and then terminates the line.
 void
println ( Object  x)
          Prints an Object and then terminates the line.
 void
println ( String  x)
          Prints a String and then terminates the line.

 
BufferedReader
 

int
read ()
          Reads a single character.
  String
readLine ()
          Reads a line of text.
void
close ()
          Closes the stream and releases any system resources associated with it.

 
 
BufferedWriter
 

void
write (char[] cbuf, int off, int len)
          Writes a portion of an array of characters.
 void
write ( String  s, int off, int len)
          Writes a portion of a String.
void
close ()
          Closes the stream, flushing it first.
 void
flush ()
          Flushes the stream.

 
 
InputStreamReader
 

Constructor Summary
InputStreamReader ( InputStream  in)
          Creates an InputStreamReader that uses the default charset.
 

 
 
OutputStreamWriter
 

Constructor Summary
OutputStreamWriter ( OutputStream  out)
          Creates an OutputStreamWriter that uses the default character encoding.
 

 
 

(三)IO 編程的通常流程:

1. 建立基本流
2. 升級基本流到高級流
3. 使用在高級流中的方法做讀寫操做
4. 關閉流並釋放資源
-------------------------------------------------------------------------------
1. Creat node stream;
2. Upgrade node stream to processing stream if necessary
3. Use the methods in the stream object to read or write
4. Close the stream and release the resource
------------------------------------------------------------------------------
1. Create InputStream/Reader
2. Upgrade to Buffered
3. Use readLine()
   While((str=in.readln())!=null)
4. close()
------------------------------------------------------------------------------
1. Create OutputStream/Writer
2. Upgrade to PrintWriter
3. Use println()
4. close()
 
 
(四)經典的IO代碼(須要背誦在心)
 
 
 
import  java.io. * ;
/*
1. Creat node stream;
2. Upgrade node stream to processing stream if necessary
3. Use the methods in the stream object to read or write
4. Close the stream and release the resource
--------------------------------------------------------
1. Create InputStream/Reader
2. Upgrade to Buffered
3. Use readLine()
   While((str=in.readln())!=null)
4. close()
--------------------------------------------------------
1. Create OutputStream/Writer
2. Upgrade to PrintWriter
3. Use println()
4. close()
*/

public   class  IOProcessSample {

 
public static void main(String[] args) {
  
//Create a file based on the first command-line argument to the program
  File file= new File(args[0]);
  
//Create buffered reader from the standard input
  BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    
  System.out.println(
"Press ctr-d or ctr-z to end");
  String str;
  
try{
   
//Create a print write to write on a file
   
//PrintWriter is required to handled the IO exception
   PrintWriter out= new PrintWriter(file);
   
//Read from the standard input and write to the file
   while((str=in.readLine())!=null){
    out.println(str);
   }

   
//close the stream and release the resource
   in.close();
   out.close();
  }

  
catch(FileNotFoundException e){
   System.err.println(
"File not found in part 1 : "+file);
  }

  
catch (IOException e){
   e.printStackTrace();
  }

  
finally{
   System.out.println(
"-----------Part1 is ended-----------------------");
  }

  
//////////////////////////////////////////////////////////////////////////////
  try{
   
//Create a buffer reader from a file
   in=new BufferedReader(new FileReader(file));
   
//Read the file and print the content on the screen.
   while((str=in.readLine())!=null){
    System.out.println(str);
   }

   
//close the stream and release the resource
   in.close();
  }

  
catch (FileNotFoundException e){
   System.err.println(
"File not found in part 2: "+file);
  }

  
catch (IOException e){
   e.printStackTrace();
  }

  
finally{
   System.out.println(
"----------------------The End -------------------------");
  }

 }

}
相關文章
相關標籤/搜索