这是我的文件 C://test.txt 组成 ACBDE FGHIJ
我想从F一直读到J。 所以输出是FGHIJ。我将如何在使用偏移量读取的 InputStream 中做到这一点。 这是我的部分实现。
InputStream is = null;
byte[] buffer = null;
char c;
try {
is = new FileInputStream("D://test.txt");
buffer = new byte[is.available()];
System.out.println("Characters printed:");
is.read(buffer, 5, 5);
for (byte b : buffer) {
if (b == 0)
// if b is empty
c = '-';
else
c = (char) b;
System.out.print(c);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null)
is.close();
}
请帮助我解决我的问题:D
最佳答案
offset
read()
的参数是缓冲区的偏移量,而不是文件的偏移量。您正在寻找的是seek()
方法,后跟 read()
偏移量为零。
注意,这是 available()
的典型滥用。 。请参阅 Javadoc。有一个特定的警告,反对将其用作输入流的长度。
关于java - 如何读取InputStream中间的偏移量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34938528/