现在的位置: 首页 > 综合 > 正文

java I/O流之字符流

2013年12月01日 ⁄ 综合 ⁄ 共 4452字 ⁄ 字号 评论关闭

转自:http://blog.csdn.net/zhuruoyun/article/details/8174510

I :input,输入 ,O:output,输出 。I/O处理技术是Java语言中实现文件操作、内存操作、控制台输入以及网络编程的基础。

IO类

java.io

为了使输入和输出的结构保持统一,从而方便程序员使用IO相关的类,在Java语言的IO类设计中引入了一个新的概念——Stream(流)。

1、输入流(Input Stream):

该类流将外部数据源的数据转换为流,程序通过读取该类流中的数据,完成对于外部数据源中数据的读入。

 2、输出流(Output Stream):

该类流完成将流中的数据转换到对应的数据源中,程序通过向该类流中写入数据,完成将数据写入到对应的外部数据源中。

在java.io包中又实现了两类流:字节流(byte stream)和字符流(char stream)。

这两种流实现的是流中数据序列的单位,在字节流中,数据序列以byte为单位,也就是流中的数据按照一个byte一个byte的顺序实现成流,对于该类流操作的基本单位是一个byte;而对于字节流,数据序列以char为单位,也就是流中的数据按照一个char一个插入的顺序实现成流,对于该类流操作的基本单位是一个char。

字符输入流(Reader体系):

Reader体系中读取数据的基本单位是字符(Char),就是每次至少读取数据一个字符(两个字节)的数据。

体系常用方法:

 void close()   关闭该流并释放与之关联的所有资源。 
 int read()    读取单个字符。 
 int read(char[] cbuf)    将字符读入数组。 
 int read(char[] cbuf, int off, int len)     将字符读入数组的某一部分。 
 int read(CharBuffer target)    试图将字符读入指定的字符缓冲区。  

注意的是,创建Reader实例对象,和调用上述方法均会抛出IOException异常。

字符输出流(Writer体系):

 Writer体系中的类写入数据的基本单位是字符(char),也就是每次最少写入一个字符(两个字节)的数据。

体系常用方法

 Writer append(char c)      将指定字符添加到此 writer。 
 Writer append(CharSequence csq)    将指定字符序列添加到此 writer。 
 Writer append(CharSequence csq, int start, int end)   将指定字符序列的子序列添加到此 writer.Appendable。 
 void close()      关闭此流,并在关闭之前刷新流。 
 void flush()       刷新该流的缓冲。 
 void write(char[] cbuf)     写入字符数组。 
 void write(char[] cbuf, int off, int len)         写入字符数组的某一部分。 
 void write(int c)         写入单个字符。 
 void write(String str)         写入字符串。 
 void write(String str, int off, int len)        写入字符串的某一部分。 

练习1:

  1. import java.io.*;  
  2. public class IODemo1{  
  3.     public static void main(String[] args){   
  4.     writTest();  
  5.     readTest();           
  6.     }  
  7.     public static void writTest(){  
  8.         FileWriter fw = null;//先定义fw的类型,并初始化为null  
  9.         try{  
  10.             fw=new FileWriter("demo.txt"); //new FileWriter的实例对象  
  11.             fw.write("写入啊发把灯关掉非常v字的发的发的噶短发");             
  12.         }catch(IOException e){  
  13.             throw new RuntimeException("写入错误!");  
  14.         }  
  15.         //必须执行close方法  
  16.         finally{  
  17.             //close 也会抛异常,所以需要try catch。  
  18.             try{  
  19.                 //当且仅当fw对象创建成功时才需要关闭资源。  
  20.                 if(fw !=null)  
  21.                     fw.close();  
  22.             }  
  23.             catch(IOException e){}  
  24.         }  
  25.     }  
  26.     public static void readTest(){  
  27.         FileReader fr = null;//先定义fr的类型,并初始化为null  
  28.         try{  
  29.             fr = new FileReader("demo.txt");  
  30.             char[] fileBuff = new char[1024];//定义一个字符数组缓冲区,大小为2kB  
  31.             int len = 0//读取的字符长度  
  32.             while((len=fr.read(fileBuff))!=-1){ //当read返回 -1时,即代表字符读取完毕。  
  33.                 System.out.print(new String(fileBuff,0,len));  
  34.             }  
  35.         }catch(IOException e){  
  36.             throw new RuntimeException("读取错误!");  
  37.         }finally{  
  38.             try{  
  39.             //当且仅当fr对象创建成功时才需要关闭资源。  
  40.                 if(fr !=null)  
  41.                     fr.close();  
  42.             }  
  43.             catch(IOException e){}  
  44.         }  
  45.           
  46.     }  
  47. }  

练习2,复制文本文件:

  1. import java.io.*;  
  2. public class FileCopyTest{  
  3.     public static void main(String[] args){  
  4.         copyFile("TestMain6.java","TestMain6.java.txt");  
  5.     }  
  6.     public static void copyFile(String from,String to){   
  7.         FileWriter fw = null;  
  8.         FileReader fr = null;  
  9.         try{  
  10.             fr = new FileReader(from);  
  11.             fw = new FileWriter(to);  
  12.             char[] buff = new char[1024]; //2kB  
  13.             int len = 0;  
  14.             while(len!=-1){  
  15.                 len = fr.read(buff);  
  16.                 if(len == -1){  
  17.                     System.out.println("复制结束.");  
  18.                     break;}  
  19.                 fw.write(buff,0,len);  
  20.                 System.out.println("正在复制...");  
  21.             }  
  22.               
  23.         } catch(IOException e){   
  24.             throw new RuntimeException("读写异常!");  
  25.         } finally {  
  26.             try {  
  27.                 if(fr != null)  
  28.                     fr.close();  
  29.             }catch (IOException e){   
  30.             throw new RuntimeException("异常!");  
  31.             }  
  32.             try {  
  33.                 if(fw != null)  
  34.                     fw.close();  
  35.             }catch (IOException e){   
  36.             throw new RuntimeException("异常!");  
  37.             }             
  38.         }         
  39.     }  
  40. }  


练习3,模拟BufferedReader:

  1. class MyBufferedReader extends Reader{  //继承Reader方法  
  2.     private Reader r;     
  3.     MyBufferedReader(Reader r){  //接受Reader的实例流对象  
  4.         this.r = r;  
  5.     }     
  6.     public String readLine() throws IOException{  //模拟行读取  
  7.         int ch =0;  
  8.         StringBuilder sb = new StringBuilder(); //用StringBuilder来模拟缓冲  
  9.         while((ch=r.read())!=-1){  
  10.             if(ch =='\r')  

抱歉!评论已关闭.