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

 
 
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.
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代码(需要背诵在心)
 
 
Java IO Stream 总结_职场 
Java IO Stream 总结_职场
import java.io.*;
Java IO Stream 总结_编程语言_03
/*
Java IO Stream 总结_休闲_051. Creat node stream;
Java IO Stream 总结_休闲_052. Upgrade node stream to processing stream if necessary
Java IO Stream 总结_休闲_053. Use the methods in the stream object to read or write
Java IO Stream 总结_休闲_054. Close the stream and release the resource
Java IO Stream 总结_休闲_05--------------------------------------------------------
Java IO Stream 总结_休闲_051. Create InputStream/Reader
Java IO Stream 总结_休闲_052. Upgrade to Buffered
Java IO Stream 总结_休闲_053. Use readLine()
Java IO Stream 总结_休闲_05   While((str=in.readln())!=null)
Java IO Stream 总结_休闲_054. close()
Java IO Stream 总结_休闲_05--------------------------------------------------------
Java IO Stream 总结_休闲_051. Create OutputStream/Writer
Java IO Stream 总结_休闲_052. Upgrade to PrintWriter
Java IO Stream 总结_休闲_053. Use println()
Java IO Stream 总结_休闲_054. close()
Java IO Stream 总结_程序员_20
*/

Java IO Stream 总结_编程语言_21
public class IOProcessSample{
Java IO Stream 总结_休闲_05
Java IO Stream 总结_职场_24 
public static void main(String[] args) {
Java IO Stream 总结_休闲_05  
//Create a file based on the first command-line argument to the program
Java IO Stream 总结_休闲_05
  File file= new File(args[0]);
Java IO Stream 总结_休闲_05  
//Create buffered reader from the standard input
Java IO Stream 总结_休闲_05
  BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Java IO Stream 总结_休闲_05    
Java IO Stream 总结_休闲_05  System.out.println(
"Press ctr-d or ctr-z to end");
Java IO Stream 总结_休闲_05  String str;
Java IO Stream 总结_职场_33  
try{
Java IO Stream 总结_休闲_05   
//Create a print write to write on a file
Java IO Stream 总结_休闲_05   
//PrintWriter is required to handled the IO exception
Java IO Stream 总结_休闲_05
   PrintWriter out= new PrintWriter(file);
Java IO Stream 总结_休闲_05   
//Read from the standard input and write to the file
Java IO Stream 总结_程序员_39
   while((str=in.readLine())!=null){
Java IO Stream 总结_休闲_05    out.println(str);
Java IO Stream 总结_编程语言_42   }

Java IO Stream 总结_休闲_05   
//close the stream and release the resource
Java IO Stream 总结_休闲_05
   in.close();
Java IO Stream 总结_休闲_05   out.close();
Java IO Stream 总结_编程语言_42  }

Java IO Stream 总结_编程语言_47  
catch(FileNotFoundException e){
Java IO Stream 总结_休闲_05   System.err.println(
"File not found in part 1 : "+file);
Java IO Stream 总结_编程语言_42  }

Java IO Stream 总结_休闲_51  
catch (IOException e){
Java IO Stream 总结_休闲_05   e.printStackTrace();
Java IO Stream 总结_编程语言_42  }

Java IO Stream 总结_程序员_55  
finally{
Java IO Stream 总结_休闲_05   System.out.println(
"-----------Part1 is ended-----------------------");
Java IO Stream 总结_编程语言_42  }

Java IO Stream 总结_休闲_05  
//////////////////////////////////////////////////////////////////////////////
Java IO Stream 总结_编程语言_60
  try{
Java IO Stream 总结_休闲_05   
//Create a buffer reader from a file
Java IO Stream 总结_休闲_05
   in=new BufferedReader(new FileReader(file));
Java IO Stream 总结_休闲_05   
//Read the file and print the content on the screen.
Java IO Stream 总结_编程语言_65
   while((str=in.readLine())!=null){
Java IO Stream 总结_休闲_05    System.out.println(str);
Java IO Stream 总结_编程语言_42   }

Java IO Stream 总结_休闲_05   
//close the stream and release the resource
Java IO Stream 总结_休闲_05
   in.close();
Java IO Stream 总结_编程语言_42  }

Java IO Stream 总结_程序员_72  
catch (FileNotFoundException e){
Java IO Stream 总结_休闲_05   System.err.println(
"File not found in part 2: "+file);
Java IO Stream 总结_编程语言_42  }

Java IO Stream 总结_Java_76  
catch (IOException e){
Java IO Stream 总结_休闲_05   e.printStackTrace();
Java IO Stream 总结_编程语言_42  }

Java IO Stream 总结_Java_80  
finally{
Java IO Stream 总结_休闲_05   System.out.println(
"----------------------The End -------------------------");
Java IO Stream 总结_编程语言_42  }

Java IO Stream 总结_编程语言_42 }

Java IO Stream 总结_程序员_20}