JAVA IO中的mark reset
2008-10-17 15:21
225 查看
mark就像书签一样,在这个BufferedReader对应的buffer里作个标记,以后再调用reset时就可以再回到这个mark过的地方。mark方法有个参数,通过这个整型参数,你告诉系统,希望在读出这么多个字符之前,这个mark保持有效。读过这么多字符之后,系统可以使mark不再有效,而你不能觉得奇怪或怪罪它。这跟buffer有关,如果你需要很长的距离,那么系统就必须分配很大的buffer来保持你的mark。
//eg.
//reader is a BufferedReader
reader.mark(50);//要求在50个字符之内,这个mark应该保持有效,系统会保证buffer至少可以存储50个字符
int a = reader.read();//读了一个字符
int b = reader.read();//又读了一个字符
//做了某些处理,发现需要再读一次
reader.reset();
reader.read();//读到的字符和a相同
reader.read();//读到的字符和b相同
例子:
public static void main(String[] args) throws IOException {
String s = "This is a &aa not./n";
byte buf[] = s.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(buf);
BufferedInputStream f = new BufferedInputStream(in);
int c;
boolean marked = false;
while ((c = f.read()) != -1) {
switch (c) {
case '&':
if (!marked) {
f.mark(1);// 为什么mark里放任何值结果都一样
marked = true;
} else {
marked = false;
}
break;
case ' ':
if (marked) {
f.reset();
System.out.print("&");
} else
System.out.print((char) c);
break;
default:
if (marked)
System.out.print((char) c);
break;
}
}
}
//eg.
//reader is a BufferedReader
reader.mark(50);//要求在50个字符之内,这个mark应该保持有效,系统会保证buffer至少可以存储50个字符
int a = reader.read();//读了一个字符
int b = reader.read();//又读了一个字符
//做了某些处理,发现需要再读一次
reader.reset();
reader.read();//读到的字符和a相同
reader.read();//读到的字符和b相同
例子:
public static void main(String[] args) throws IOException {
String s = "This is a &aa not./n";
byte buf[] = s.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(buf);
BufferedInputStream f = new BufferedInputStream(in);
int c;
boolean marked = false;
while ((c = f.read()) != -1) {
switch (c) {
case '&':
if (!marked) {
f.mark(1);// 为什么mark里放任何值结果都一样
marked = true;
} else {
marked = false;
}
break;
case ' ':
if (marked) {
f.reset();
System.out.print("&");
} else
System.out.print((char) c);
break;
default:
if (marked)
System.out.print((char) c);
break;
}
}
}
相关文章推荐
- java.io.IOException: mark/reset not supported
- 文件读取IO异常java.io.IOException: mark/reset not supported at java.io.InputStream.reset
- [j2se]java.io中的mark与reset
- Java IO mark() reset()
- java.io.IOException: mark/reset not supported
- spring+ibatis问题1—— 程序报错:java.sql.SQLException: Io 异常: Connection reset by peer, socket write error; ”或“java.sql.SQLException 关闭的连接”异常
- JAVA中mark()和reset()用法
- java 文件流重定位,seek ,mark reset.
- java.io.IOException: Connection reset by peer
- java.io.IOException Connection Reset by Peer 的原因是什么
- JAVA中mark()和reset()用法
- IO中 mark 和reset 的使用
- java.io.ByteArrayInputStream的mark方法
- JAVA中mark()和reset()用法
- JAVA中mark()和reset()用法
- java.io.IOException: Connection reset by peer问题处理
- java.io.IOException: mark() not supported
- java.io.IOException Connection Reset by Peer
- 在was控制台重启工程时,zookeeper报以下错误:java.io.IOException:Connection reset by peer
- 在linux上通过JDBC连接ORACLE 时总是出现 java.sql.SQLRecoverableException: IO Error: Connection reset 的问题