• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading and Writing to a File  RSS feed

 
James Laison
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a problems which i will like assistance from you guys.

I am using FileInputStream and FileOutputStream to read and write from a file. They both worked but i am having problems with both the Reading and the Writing.

Reading with FileInputStream: I noticed that i can't specify the read of the file i want to read and that i can only read from the beginning of the file. In the case i will like to know what int off stands for in the method read(byte[] b, int off, int len).

Writing to a File with FileOutputStream: When i write to a file with FileOutputStream, i observed that it delete the contents of the file if it already exists. I want to be able to append new data to the contents of the file not replace it with the new one.

The type of files i intend reading are exe files,media files and so on which i believe must be ready in raw bytes.

Please i need urgent assistance asap and also demo codes will be appreciated.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello James-

Welcome to JavaRanch.

Like the FileInputStream javadoc states, the offset parameter allows you to start reading at a different location into the file than the beginning. It sounds like that is what you are looking for.

You can append to a file by using the FileOutputStream constructor that takes an additional boolean parameter. The javadoc explains that in detail.

As to the urgency of your request, EaseUp. We're volunteers, and most questions need fast answers.
 
James Laison
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your prompt response. As for the int off in read(byte[] b, int off, int len). I understand it to be the right method. But hvae you tried the method? If not i will like you to try it and see if you will get the same problem.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The usually well-informed beertender Ulf Dittmer has grabbed the wrong end of the stick in regards to read and the off parameter. You should carefully read the documentation for this method:

http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html

In summary, off and len are indicating a target segment of b -- the first byte read, if any, would be stored in b[off]. As far as *where* these bytes come from in the input stream, they are just the next bytes in sequence -- this does not reposition from where you will read.

If you want to start reading somewhere else in a file, you can try the skip method -- but read it's API -- it's chock full of conditions about not always working as expected. If you really need to jump around in a binary file, consider using RandomAccessFile.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stand corrected. The javadocs for FileInputStream are much terser than the ones for InputStream, and can -if I may say so- easily be interpreted incorrectly.
 
James Laison
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks very much for the response. I will try out RandomAccessFile and post the outcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!