📜  Java I/O-PushbackReader类

📅  最后修改于: 2020-09-27 07:22:24             🧑  作者: Mango

Java PushbackReader类

Java PushbackReader类是字符流读取器。它用于将字符推回流中并覆盖FilterReader类。

类声明

我们来看一下java.io.PushbackReader类的声明:

public class PushbackReader extends FilterReader

类方法

Method Description
int read() It is used to read a single character.
void mark(int readAheadLimit) It is used to mark the present position in a stream.
boolean ready() It is used to tell whether the stream is ready to be read.
boolean markSupported() It is used to tell whether the stream supports mark() operation.
long skip(long n) It is used to skip the character.
void unread (int c) It is used to pushes back the character by copying it to the pushback buffer.
void unread (char[] cbuf) It is used to pushes back an array of character by copying it to the pushback buffer.
void reset() It is used to reset the stream.
void close() It is used to close the stream.

PushbackReader类的示例

import java.io.*;
public class ReaderExample{
public static void main(String[] args) throws Exception {
        char ary[] = {'1','-','-','2','-','3','4','-','-','-','5','6'};
        CharArrayReader reader = new CharArrayReader(ary); 
        PushbackReader push = new PushbackReader(reader);
        int i;
            while( (i = push.read())!= -1) {
                if(i == '-') {
                int j;
                    if( (j = push.read()) == '-'){
                         System.out.print("#*");
                    }else {
                    push.unread(j); // push back single character
                        System.out.print((char)i);
                    }
            }else {
                    System.out.print((char)i);
            }
           }       
    }
}

输出量

1#*2-34#*-56