CharArrayReader和CharArrayWriter实例

作者在 2010-05-21 15:19:00 发布以下内容
import java.io.*;
public class TestCharArrayReader{
    public static void main(String[] args){
        char[] b = {'I',' ','a','m',' ','S','a','i','l','i','n','g','!'};
        CharArrayReader car = new CharArrayReader(b);
        MyReader mr = new MyReader();
        mr.getInfo(car);    
    }    
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.io.*;
public class MyReader{
    public void getInfo(Reader reader){
        try{
            int b = reader.read();
            while(b != -1){
                System.out.print((char)b);
                b = reader.read();    
            }
            System.out.println();
        }catch(IOException e){
            e.printStackTrace();    
        }        
    }    
}
 
import java.io.*;
import java.util.Date;

public class TestCharArrayWriter{
    public static void main(String[] args){
        try{
            BufferedReader br = new BufferedReader(new FileReader("a.txt"));
            String s ;
            while((s=br.readLine()) != null){
                System.out.println(s);    
            }
        }catch(IOException e){
            CharArrayWriter cw = new CharArrayWriter();
            PrintWriter pw = new PrintWriter(cw,true);
             e.printStackTrace(pw);
             //char[] ca = cw.toCharArray();
             String info = cw.toString();
             Date time = new Date();
             //将time和info信息写入数据库---    
             System.out.println("出错时间: " + time);
             System.out.println("错误信息:\n" + info);
        }            
    }    
}
 
I/O编 | 阅读 1432 次
文章评论,共0条
游客请输入验证码
浏览290922次