How to Read a File from a Certain Offset in Java

How to read a file from a certain offset in Java?

RandomAccessFile exposes a function:

seek(long pos) 
Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.

Java how to read part of file from specified position of bytes?

Take a look at java.io.RandomAccessFile.seek(). It allows reading a file from an arbitrary position, but its constructor requires a filename or a File object, not just an input stream as your method currently receives.

How do I fetch specific bytes from a file knowing the offset and length?

Use RandomAccessFile.seek() to position to where you want to read from and RandomAccessFile.readFully() to read a full byte array.

byte[] magic = new byte[4];
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.seek(0L);
raf.readFully(magic);
System.out.println(new String(magic));

The problem with your code is that when you create the file in read-write mode, most likely the file pointer points to the end of the file. Use the seek() method to position.

Also you can use the RandomAccessFile.read(byte[] b, int off, int len) method too, but the offset and length corresponds to the offset in the array where to start storing the read bytes, and length specifies how many bytes to read from the file. But the data will still be read from the current position of the file, not from the off position.

So once you called seek(0L);, this read method also works:

raf.read(magic, 0, magic.length);

Also note that the read and write methods will automatically move the current position, so for example seeking to 0L, then reading 4 bytes (your magic word) will result in the current pointer being moved to 4L. This means you can call read methods subsequently without having to seek before each read and they will read a continuous portion of the file increasing by position, they will not read from the same position.

Last Note:

When creating a String from a byte array, quoting from the javadoc of String(byte[] bytes):

Constructs a new String by decoding the specified array of bytes using the platform's default charset.

So the platform's default charset will be used which may be different on different platforms. Always specify a correct encoding like this:

new String(magic, StandardCharsets.UTF_8);

Java - read bytes at given offset

I think that java.io.RandomAccessFile is your new friend :-)

Beware of the following code, it has not been tested.

RandomAccessFile raf = new RandomAccessFile("foo.bin", "r");
raf.seek(0xd768);
int value = raf.read();

java filereader read at offset

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();

Reading a file in a directory in java and storing its offset in the directory

Okay I solved my problem on what I wanted to do. I created an offset to each file I stored in a textfile by taking the length + 1 of each filename and adding it to the offset then writing it to a file.

 public static void readOffsets()
{
try {
File file = new File("indexFile.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);

FileWriter fw = new FileWriter("indexFile2.txt");
PrintWriter pw = new PrintWriter(fw);

String line;
int offset = 0;
pw.write(offset + "\n");
while((line = br.readLine()) != null)
{
int length = line.length();
offset += length + 1;
pw.write(offset + "\n");

}
pw.close();
br.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}

Then when seeking the offset using the randomaccessfile I would get the correct filename.

 public static void seekOffset()
{
try {
RandomAccessFile file = new RandomAccessFile("IndexFile.txt", "r");
file.seek(693);
System.out.println(file.readLine());
}
catch(IOException ioe)
{

}
}

How to set an offset with readline in java?

readline doesn't provide such functionality. You can use seek function to set an offset (link to related question. However, it has no way of knowing about newline symbols or anything else. It simply sets offset in bytes. If length of your lines is fixed - you can use it just like you want. Otherwise - you need to use readline several times to get required string.



Related Topics



Leave a reply



Submit