发现java io流中mark()和reset()方法没用,求曾姐····

xiaoxiami002 2011-04-18 09:03:51

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;

public class chinese_english_inputstream {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("e:/io流专用文件/da1.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] b = new byte[8];
bis.read(b);
ByteArrayInputStream fais = new ByteArrayInputStream(b);
fais.mark(1); //这里不管是几 控制台输出的都是第一个字符.
fais.reset();
System.out.println((char)fais.read());
}
}



这个也一样

import java.io.BufferedInputStream;
import java.io.FileInputStream;

public class chinese_english_inputstream {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("e:/io流专用文件/da1.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
bis.mark(5);
bis.reset();
System.out.println((char)bis.read());
}
}


《da1.txt》:
ab中国人



求解啊。。。。。。
...全文
387 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lqb0013 2011-04-23
  • 打赏
  • 举报
回复
java板块人怎么这么少。。。
xiaoxiami002 2011-04-21
  • 打赏
  • 举报
回复
求高人指点
xiaoxiami002 2011-04-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 humanity 的回复:]

翻译一下 javadocs 里面关于 InputStream#mark 方法的注释,不够了准确,但大概意思是说,如果 InputStream.markSupprted() 返回 true 表示能够标记流的当前位置,参数是要求缓冲的最多字节数,比如,在读取了第5个字节之后,mark(10) 的话,我之后 reset 再来读取的话,这次读取到的前10个字节就是之前从第5个字节开始的10个字节。它能让……
[/Quote]

非常感谢你的回答 ,mark()方法里的参数是缓冲的最多字节数,但我发现不管参数是几效果都一样:

import java.io.BufferedInputStream;
import java.io.FileInputStream;

public class chinese_english_inputstream {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("e:/io流专用文件/da1.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read();
bis.read();
bis.mark(1); //我这设置的缓冲区为1个字节
bis.read();
bis.read();
bis.read();
bis.reset(); //在读到e后进行reset
System.out.println((char)bis.read()+"-"+(char)bis.read()+"-"+(char)bis.read());
}
}


《da1.txt》:
abcdefg中国人

问题:
控制台的输出是:c-d-e
按你说的那该是:c-f-g才对啊?
humanity 2011-04-19
  • 打赏
  • 举报
回复
翻译一下 javadocs 里面关于 InputStream#mark 方法的注释,不够了准确,但大概意思是说,如果 InputStream.markSupprted() 返回 true 表示能够标记流的当前位置,参数是要求缓冲的最多字节数,比如,在读取了第5个字节之后,mark(10) 的话,我之后 reset 再来读取的话,这次读取到的前10个字节就是之前从第5个字节开始的10个字节。它能让我们反复地读取同一段内容。


[Quote=引用 2 楼 xiaoxiami002 的回复:]

引用 1 楼 alinaqvi 的回复:

Seems your understanding of mark() method is incorrect. In fact stream marks are intended to be used in situations where you need to read ahead a little to see what’s in the s……
[/Quote]
xiaoxiami002 2011-04-19
  • 打赏
  • 举报
回复
xiaoxiami002 2011-04-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 alinaqvi 的回复:]

Seems your understanding of mark() method is incorrect. In fact stream marks are intended to be used in situations where you need to read ahead a little to see what’s in the stream.

mark() method ……
[/Quote]

英文太挫,没理解。
求会中文的高手。。。
Ali 2011-04-18
  • 打赏
  • 举报
回复
Seems your understanding of mark() method is incorrect. In fact stream marks are intended to be used in situations where you need to read ahead a little to see what’s in the stream.

mark() method marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

I think you are confusing mark method with skip.

Hope it helps.

//Ali

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧