wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes RandomAccessFile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "RandomAccessFile " Watch "RandomAccessFile " New topic
Author

RandomAccessFile

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635

Suppose I have a txt file that have these strings:

abalfazl hossein



I want to read this file so the file pointer points to "hossein" , then read to "s"(hos)
How to adjust fseek()?
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2864
    
  11

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?
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


I have a file, I typed in that file abalfazl hossein, and It returns this:

8
hossein


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.


and

seek

public void seek(long pos)
throws IOException
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.
Parameters:
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.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Java Strings use 2 bytes per character. How much bytes per character are used in files depends on the encoding. If the file is a simple ASCII file, then it will take 1 byte per character.

It's generally not a good idea to mix raw input operations (like seek) with character operations, unless you're very familiar with the file format.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Thank you very much!

What is the definition for "raw input operations"?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RandomAccessFile