Well, it would depend if your file was plain ASCII text or Unicode characters, but the basic idea is the same. Open up the file in read or read write mode, seek() some number of bytes (there is no fseek) then read the three bytes or three characters you need. Where are you running into a problem?
Joined: Sep 06, 2007
I have a file, I typed in that file abalfazl hossein, and It returns this:
It works fine but I have question:
public int length()
Returns the length of this string. The length is equal to the number of 16-bit Unicode characters in the string.
public void seek(long pos)
Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change the file length. The file length will change only by writing after the offset has been set beyond the end of the file.
pos - the offset position, measured in bytes from the beginning of the file, at which to set the file pointer.
If pos is measured in bytes, Then every character in JAVA is 2 bytes,Then for "abalfazl", It is 16.
Then How it works?
I used 8 in seek method.
There's no real definition, it's just a phrase I used. I meant operations that work without regard to what data means. A file, whether it's a text document, a PDF, an image or something else, is just a collection of bytes, as far as FileChannel is concerned. It doesn't know what the bytes stand for. Mixing operations of a class like FileChannel with operations that structure the bytes in some way, can be dangerous if you don't know the details of how the file is formatted.
For instance, in your example, you can use a Reader to extract the characters from your file, but you don't know whether the file would be positioned at 8 or 16 (or even something else) after reading those characters, unless you know the format or encoding of the file.
Look up what character encoding means. UTF-16 always uses 2 bytes per character. ASCII always uses 1 byte per character (but it can't handle a lot of characters). Others can have 1, 2 or even a mix of 1 and 2.