• Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginner question: having DataOutputStream and RandomAccessFile

 
Rudy Koento
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is it ok to have DataOutputStream and RandomAccessFile both accessing the same file?

I'm having:
File f = new File("myfile.txt");
DataOutputStream dos = DataOutputStream(f,true);
RandomAccessFile raf = RandomAccessFile(f);

I'm actually doing my assignment, and I'm not sure whether doing the above is a good thing. But I do need to append to the file and also do a random access read. Is there any other I/O library that I can use?

thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd open random access only - probably your only real choice. To append, try to seek to file length and write. ANd let us know how it works out for you.
 
Rudy Koento
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply. All these while I thought RandomAccessFile cannot do append, because it has the EOFException thing. That's why I do a DataOutputStream... I guess I didn't read the javadoc properly...

Still, in any case, is it good to have 2 streams pointing to the same file? Or is it something that should never been done? maybe not thread friendly, etc..?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two read-write streams is probably not allowed. Give it a try and see One updating and one read only would be a bit tricky but non-Java programs do it all the time. Again, try it and see if Java lets you. Learning from experience is the fun part!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!