如何使用 FileReader.read() 读取特定偏移量处的字节?
FileReader fr = new FileReader(path);
char[] tmp = null;
fr.read(tmp, 11, 1);
n = tmp.toString();
n = Integer.parseInt(n,16);
尽管“path”中的文件有效且不为空,但此代码返回 nullpointerException。我打算在这里做的是读取该文件中的第十一个字节。
使用 BufferedReader.readLine() 从文件中读取行在同一个文件上效果很好,但我不知道如何获取从特定偏移量开始的特定字节数。
最佳答案
在read(char[] buf, int offset, int length)
中,offset是buf数组中的偏移量。
您需要的是跳过偏移字符。
FileReader fr = new FileReader(path);
int offset = 11;
fr.skip(11);
int c = fr.read();
关于java filereader读取偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28786081/