简体   繁体   English

Java 是否将读取的 position 保存在 InputStream 中?

[英]Does Java save the read position in the InputStream?

I'm in the process of reading data, does Java 'save' your bytes read, or do I have to use an offset?我正在读取数据,Java“保存”你读取的字节,还是我必须使用偏移量?

A FileInputStream does indeed save your position. FileInputStream 确实可以保存您的 position。

If you have a file with 3 bytes, 0xff 0x00 0x0c , calling:如果你有一个 3 字节的文件, 0xff 0x00 0x0c ,调用:

System.out.println(fis.read());
System.out.println(fis.read());
System.out.println(fis.read());

Will output:请问output:

255
0
12

You just to mirror @WhiteFang's solution for writing.您只是为了反映@WhiteFang 的写作解决方案。

FileInputStream fis = new FileInputStream(files[0]);
DataInputStream dis = new DataInputStream(new BufferedInputStream(fis));
int numFiles = dis.readInt();
int numBytesInName = dis.readInt();
String filename = dis.readUTF();
long numBytesInFile = dis.readLong();
// loop to read bytes into a byte[]

BTW, using writeUTF/readUTF makes writing the length of the file name redundant.顺便说一句,使用 writeUTF/readUTF 会使写入文件名的长度变得多余。 Additionally, you don't need to record the number of files if you are not going to write anything after this information.此外,如果您不打算在此信息之后写任何内容,则无需记录文件数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM