java温故而知新(6)深入理解IOStream

12/12/2015来源:Java教程人气:2854

一、什么是IO Stream

Stream 是在编程语言中对输入输出的总称 (一种比喻的称谓。Stream 为流水,输入输出实际上就是指数据的流动,数据由一个地方运动的另一个地方,就像流水一样,程序员将输入输出比作流水,再恰当不过了)。
 流按照其所载内容分类,大致可分为字节流和字符流两大类。
1、字节流 Byte Stream
在计算机中,byte是相当于机器语言中的单词,他在java中统一由InputStreamOutputStream作处理。
 
2、字符流(Character Stream
而在编码体系中,一般采用Char2 bytes他在Java中统一由ReaderWriter作处理。
 
InputStream, OutputStream, ReaderWriter, 作为在java.io.*包的顶级父类,定义了IO PRocess中最抽象的处理和规范。对于实际的应用,他们并不适用。于是根据各种实际的需要,由他们派生出来形式各样各具特色的子类。
 

二、IO 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 编程的一般流程

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