Reputation: 147
First of all I have a couple hours experience with Java so, If its a bit simple question, sorry about that.
Now, I want to read a file but I dont want to start at the beginning of file instead I want to skip first 1024 bytes and than start reading. Is there a way to do that ? I realized that RandomAccessFile might be useful but I'm not sure.
try {
FileInputStream inputStream=new FileInputStream(fName);
// skip 1024 bytes somehow and start reading .
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Upvotes: 4
Views: 4816
Reputation: 201439
You could use a method like skipBytes() here
/**
* Read count bytes from the InputStream to "/dev/null".
* Return true if count bytes read, false otherwise.
*
* @param is
* The InputStream to read.
* @param count
* The count of bytes to drop.
*/
private static boolean skipBytes(
java.io.InputStream is, int count) {
byte[] buff = new byte[count];
/*
* offset could be used in "is.read" as the second arg
*/
try {
int r = is.read(buff, 0, count);
return r == count;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
Upvotes: 0
Reputation: 23667
You will want to use the FileInputStream.skip
method to seek to the point you want and then begin reading from that point. There is more information in the javadocs about FileInputStream
that you might find interesting.
Upvotes: 6