• Post Reply Bookmark Topic Watch Topic
  • New Topic

read from file  RSS feed

 
Andrew Lit
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
what should i do if i want to read from the exact place of the file? currently i use:

What i want is to read one byte from the 200th place let's say.
thank you.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The skip() method is quickest. However it doesn't guarantee that the requested number of bytes will actually be skipped (since it's possible something in the stream will block, the method returns early to let you do something else if you prefer). To make sure the desired number of bytes are really skipped, try something like this:

Note also that I used an inStream (InputStream) rather than a Reader. If you want to count bytes, you need to use an InputStream; if you want to count characters, use a Reader. You can't freely switch between the two unless you know the encoding of the file and are confident in how it works.
 
Andrew Lit
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you.
but i got one more question:
in my file i have such first three bytes
b8 8e e8 ........
but when i read them with filereader
i get the result:
-24 -24 -24 ....
plz show me an example how correctly to read one byte
would be really appreciated
thank you
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, if you want to talk about bytes, you need to use an InputStream rather than a Reader. Or you may want to try using a RandomAccessFile - personally I find them slow and poorly designed for anything complex, but it may be easy to use for what you want.
Anyway, reading bytes with an InputStream is easy:
 
Andrew Lit
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at last i got your idea, thank you again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!