-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushBackTest.java
More file actions
44 lines (42 loc) · 1.05 KB
/
PushBackTest.java
File metadata and controls
44 lines (42 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//将文件中的"new PushBackTest"字符串之前的内容输出出来 使用PushBackInputStream和PushBackOutputStream流
import java.io.*;
public class PushBackTest
{
public static void main(String[] args)
{
try(PushbackReader pr = new PushbackReader(new FileReader("PushBackTest.java"),64))
{
char[] a = new char[32];
int hasRead = 0;
int index = 0;
String lastcontent = "";
while ((hasRead = pr.read(a))>0)
{
String content = new String(a , 0 , hasRead);
//System.out.print(new String(a , 0 , hasRead));
if ((index = (lastcontent + content).indexOf("new PushbackReader")) > 0)
{
if (index > 32)
{
a = new char[index];
}
pr.unread((lastcontent + content).toCharArray());
pr.read(a , 0 , index);
lastcontent = new String(a , 0 , index);
System.out.print(lastcontent);
System.out.println("接下来就是关键词后面的文件内容");
break;
}
else
{
System.out.print(lastcontent);
lastcontent = content;
}
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}