• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RandomAccessFile

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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()?
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much!

What is the definition for "raw input operations"?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic