看看那个答案对呀?请说出对错原因,谢谢
26.要从文件" file.dat"文件中读出第10个字节到变量C中,下列哪个方法适合?
A.FileInputStream in=new FileInputStream("file.dat");
in.skip(9); int c=in.read();
B.FileInputStream in=new FileInputStream("file.dat");
in.skip(10); int c=in.read();
C.FileInputStream in=new FileInputStream("file.dat");
int c=in.read();
D.RandomAccessFile in=new RandomAccessFile("file.dat");
in.skip(9); int c=in.readByte();[/size]
同意LS
补充下说明:
[quote]
B.FileInputStream in=new FileInputStream("file.dat");
in.skip(10); int c=in.read();
[/quote]
这样读出的是第11个字节
[quote]
C.FileInputStream in=new FileInputStream("file.dat");
int c=in.read();
[/quote]
都没有指出从哪个字节开始
[quote]
D.RandomAccessFile in=new RandomAccessFile("file.dat");
in.skip(9); int c=in.readByte();[/size]
[/quote]
没有skip这个方法
答案是 D
错了 是A sorry
A 跳字节是从1开始数起的 从第10个字节起就是说要跳9个字节 应该是 in.skip(9) 选A
skip
public long skip(long n)
throws IOException从输入流中跳过并丢弃 n 个字节的数据。
出于各种原因,skip 方法最终跳过的字节数可能更少一些,甚至可能为 0。如果 n 为负,则抛出 IOException,即使 InputStream 超类的 skip 方法在这种情况下没有执行任何操作。返回实际跳过的字节数。
此方法跳过的字节可能多于底层文件中剩余的字节。这不会产生异常,并且跳过的字节数可能包括底层文件的 EOF(文件结束符)之后的一些字节数。如果试图在跳过末尾之后读取流,那么会返回指示文件末尾的 -1。
覆盖:
类 InputStream 中的 skip
参数:
n - 要跳过的字节数。
返回:
实际跳过的字节数。
抛出:
IOException - 如果 n 为负,或者发生 I/O 错误。
A.FileInputStream in=new FileInputStream("file.dat");
//从输入流中跳过并丢弃 n 个字节的数据。
in.skip(9);
int c=in.read();
D.RandomAccessFile in=new RandomAccessFile("file.dat");
in.skip(9); int c=in.readByte();
D看起来好像是对的,但其实RandomAccessFile没有一个参数的构造函数的
答案应该是:A