JAVA IO - 放回字节

PushbackInputStream能够在字节流中插入字符, 构造函数能够定义unread是buffer的字节数。 java

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;

public class PushbackTest {

  public static void main(String[] args)
      throws IOException {
    PushbackInputStream push = new PushbackInputStream(
        new ByteArrayInputStream("hello, world!".getBytes()), 10);
    int temp = 0;
    while ((temp = push.read()) != -1) {
      if (temp == ',') {
        push.unread("(...)".getBytes());
      }
      System.out.print((char) temp);
    }
  }
}
相关文章
相关标签/搜索