//IO-使用PushbackReader.txt1、源代码
//ReadCharacters.javaimport java.io.*;class ReadCharacters
{
  public static void main(String[] args)
  {
    try
    {
      String dirName = ".";             // Directory for the output file
      String fileName = "web";          // Name of the output file       File input = new File(dirName, fileName);  // The file object
      PushbackReader in = new PushbackReader( new BufferedReader(new FileReader(input)));
      int c;                                     // Character store
      while(true)
      {
        String number ="";                       // String length as characters
        // Get the characters for the length
        while(Character.isDigit((char)(c = in.read())))
          number += (char)c;                  if(c==-1)                                // Check for end of file
          break;                                 // End the loop
        else                                     // It is not end of file so
          in.unread(c);                          // push back the last character        char[] proverb = new char[Integer.parseInt(Number)];
        in.read(proverb);                        // Read a proverb
        System.out.println(proverb);
      }
    }
    catch(FileNotFoundException e)               // Stream creation exception
    {
      System.err.println(e);
      return;
    }
    catch(IOException e)                         // File read exception
    {
      System.err.println("Error reading input file" + e );
      return;
    }
  }
}2、文件web的内容2dd3efd4dddd5ddrrr3、输出
F:\java>javac ReadCharacters.javaF:\java>java  ReadCharacters
dd
efd
dddd
ddrrr4、文件web的内容格式很重要,否则会出错。
原因很简单,数字表示此数字后面有几个字符,
如2dd3efd4dddd5ddrrr就表示:
数字2后面有2个字符,
数字3后面有3个字符,
数字4后面有4个字符,
依此类推。如果文件web的内容格式不对,如
2dda3efd4dddd5ddrrr
则表示
数字2后面有2个字符,
紧接着应该是数字或者到了文件尾,
但现在是字符a,所以在执行到语句
char[] proverb = new char[Integer.parseInt(number)];
时number的内容为空,无法将其解析成int,故出错!!!6、改进的源代码
//ReadCharacters.java
// Using the push back reader
import java.io.*;class ReadCharacters
{
  public static void main(String[] args)
  {
    try
    {
      String dirName = ".";         // Directory for the output file
      String fileName = "web";          // Name of the output file       File input = new File(dirName, fileName);  // The file object
      PushbackReader in = new PushbackReader( new BufferedReader(new FileReader(input)));
      int c;                                     // Character store
      while(true)
      {
        String number ="";                       // String length as characters
        // Get the characters for the length
        while(Character.isDigit((char)(c = in.read())))
          number += (char)c;                  if(c==-1)                                // Check for end of file
          break;                                 // End the loop
        else                                     // It is not end of file so
          in.unread(c);                          // push back the last character
        if(number.equals("")==false){            // Deal with the legal chars in the file
          char[] proverb = new char[Integer.parseInt(number)];
          in.read(proverb);                      // Read a proverb
          System.out.println(proverb);
        }
        else
          in.read();                             //Ignore the illegal char in the file
      }
    }
    catch(FileNotFoundException e)               // Stream creation exception
    {
      System.err.println(e);
      return;
    }
    catch(IOException e)                         // File read exception
    {
      System.err.println("Error reading input file" + e );
      return;
    }
  }
}7、格式不正确的web文件内容
2love3her4isok5areyousu2re8、执行结果
F:\java>java  ReadCharacters
lo
her
isok
areyo
re