user1837293
user1837293

Reputation: 1666

java filereader read at offset

How do I use FileReader.read() to read a byte at a specific offset?

FileReader fr = new FileReader(path);
char[] tmp = null;
fr.read(tmp, 11, 1);
n = tmp.toString();
n = Integer.parseInt(n,16);

This code returns nullpointerexception although the file in 'path' is valid and not empty. What I intend to do here is to read the eleventh byte in that file.

reading lines from the file with BufferedReader.readLine() works well on the same file but I can't figure out how to get a specific number of bytes starting at a specific offset.

Upvotes: 1

Views: 1672

Answers (1)

T.Gounelle
T.Gounelle

Reputation: 6033

In read(char[] buf, int offset, int length), the offset is offset in the buf array. What you need is to skip offset characters.

FileReader fr = new FileReader(path);
int offset = 11;
fr.skip(11);
int c = fr.read();

Upvotes: 3

Related Questions