Jackie
Jackie

Reputation: 21

Java 6 File Input Output Stream (same file)

I searched and looked at multiple questions like this, but my question is really different than anything I found. I've looked at Java Docs.

How do I get the equivalent of this c file open:

stream1 = fopen (out_file, "r+b");

Once I've done a partial read from the file, the first write makes the next read return EOF no matter how many bytes were in the file.

Essentially I want a file I/O stream that doesn't do that. The whole purpose of what I'm trying to do is to replace the bytes in an existing file in the current file. I don't want to do it in a copy or make a copy before I do the Read->Write.

Upvotes: 2

Views: 1431

Answers (2)

Tom
Tom

Reputation: 19302

As Perception mentions, you can use a RandomAccessFile. Also, in some situations, a FileChannel may work better. I've used these to handle binary file data with great success.

EDIT: you can get a FileChannel from the RandomAccessFile object using getChannel.

Upvotes: 1

Perception
Perception

Reputation: 80623

You can use a RandomAccessFile.

Upvotes: 7

Related Questions