BufferedReader类 示例

/*

BufferedReader 通过缓冲输入提高性能。它有两个构造函数:
BufferedReader(Reader inputStream)
BufferedReader(Reader inputStream, int bufSize)
第一种形式创建一个默认缓冲器长度的缓冲字符流。第二种形式,缓冲器长度由bufSize传入。和字节流的情况相同,缓冲一个输入字符流同样提供支持可用缓冲器中流内反向移动的基础。为支持这点, BufferedReader 实现了mark( ) 和reset( ) 方法, 并且BufferedReader.markSupported( ) 返回true.。

*/

// Use buffered input.
import java.io.*;
class BufferedReaderDemo{
 public static void main(String[] args) throws IOException{
  String s = "This is a © copyright symbol but this is & copy not./n";
  char buf[] = new char[s.length()];
  s.getChars(0, s.length(), buf, 0);
  CharArrayReader in = new CharArrayReader(buf);
  BufferedReader f = new BufferedReader(in);
  int c;
  boolean marked = false;
  while ((c = f.read()) != -1) {
   switch(c) {
    case '&':
    if (!marked) {
     f.mark(32);
     marked = true;
    } else {
     marked = false;
    }
    break;
    case ';':
    if (marked) {
     marked = false;
     System.out.print("(c)");
    } else
     System.out.print((char) c);
    break;

    case ' ':
    if (marked) {
     marked = false;
     f.reset();
     System.out.print("&");
    } else
     System.out.print((char) c);
    break;

    default:
    if (!marked)
     System.out.print((char) c);
    break;
   }
  }
 }
}

你可能感兴趣的:(Java)