在 java 中的文件中向后跳过
Skipping backward in a file in java
我在 Java 中使用 FileInputStream
来解析输入文件,我希望能够在该文件中向后跳转。我的结果好坏参半,根据 Java 文档,这是不可能的,尽管它在程序的其他部分有效。
目前我得到一个:
'Exception in thread "main" java.io.IOException: Invalid argument'
我相信 RandomAccessFile
会产生同样的问题。有什么技巧可以使这成为可能吗?
编辑:
发生错误的示例:
FileInputStream file = new FileInputStream(inputfile);
file.read(check,0,6);
file.skip(-6);
如果你get the FileChannel associated with the FileInputStream you can set the position上了频道,这会影响下一次从流中读取。
同样,如果您使用 RandomAccessFile, you can seek 到文件中的所需位置。
我在 Java 中使用 FileInputStream
来解析输入文件,我希望能够在该文件中向后跳转。我的结果好坏参半,根据 Java 文档,这是不可能的,尽管它在程序的其他部分有效。
目前我得到一个:
'Exception in thread "main" java.io.IOException: Invalid argument'
我相信 RandomAccessFile
会产生同样的问题。有什么技巧可以使这成为可能吗?
编辑:
发生错误的示例:
FileInputStream file = new FileInputStream(inputfile);
file.read(check,0,6);
file.skip(-6);
如果你get the FileChannel associated with the FileInputStream you can set the position上了频道,这会影响下一次从流中读取。
同样,如果您使用 RandomAccessFile, you can seek 到文件中的所需位置。