回退流 Demo2
阿新 • • 發佈:2017-06-05
oid ack stream 讀取 字符 print logs style amd
package pushbackInputStream; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PushbackInputStream; /* * 自己再寫一次 */ public class PushbackInputStreamDemo2 { public static void main(String[] args) throws Exception { //定義一個字符串 String str = "hello.hai.heihei.laiba...";//定義一個內存輸入流對象 ByteArrayInputStream bai = new ByteArrayInputStream(str.getBytes()); //定義一個回退流對象 PushbackInputStream pbi = new PushbackInputStream(bai); System.out.println("讀取的數據為:"); int temp = 0; while ((temp = pbi.read())!=-1) { if (temp ==‘.‘) { pbi.unread(temp); temp= pbi.read(); System.out.print("(回退"+(char)temp+")"); } else { System.out.print((char)temp); } } } }
回退流 Demo2