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
How to Get a Spring Bean in a Servlet Filter
Does Java Support Inner/Local/Sub Methods
Port of Random Generator from C to Java
How to Encode Uri Parameter Values
How to See the Source Code of the Sun Jdk
Lambda Expression and Method Overloading Doubts
How to Find Out What Type Each Object Is in a Arraylist<Object>
Why Isn't a Qualified Static Final Variable Allowed in a Static Initialization Block
Disable Spring Security for Options Http Method
How to Format Localdate to String
How to Serialize a List in Java
Counting the Number of Files in a Directory Using Java
How to Stop Selenium from Creating Temporary Firefox Profiles Using Web Driver
Filter Java Stream to 1 and Only 1 Element
Why Isn't This Code Causing a Concurrentmodificationexception