Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing outputstream starting at specific byte, overwriting therafter. 1

Status
Not open for further replies.

jakeisstoked

Technical User
May 9, 2003
28
0
0
AU
Hi, I need a file to be written at a specific byte to a specific byte (I can write to a specific byte, that's simple), and I can also read inputstreams from and to a specific byte thanks to the skip() method. Problem is I can't seem find a similar method for outputstreams.

For example, say I have a 10kb file, I need to write to that file starting at byte 1024 and writing over until byte 4096. So I have just modified (not appended to) that file. I realise I can do this by changing what I need and writing the whole file again, however that is isn't what I want.

So is there something like skip() for outputstreams?
Thanks a million
-Jake
 
You want a RandomAccessFile. Below is a bit of code that shows its usuage :

Code:
                RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");

                raf.write("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqq".getBytes());

                raf.seek(5);
                raf.write("hello".getBytes());

                raf.close();

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
So random access file is otherwise the same as and outputstream? Anyway, thanks heaps, that's exactly what I needed. Can't believe random access didn't even spark in my head when thinking of that!

-Jake
 
When talking about file IO in C, there is no such thing as an "outputstream" or "inputstream" - there is just a file pointer (or file descriptor), to which you can seek, read and write to/from.

In Java, File objects don't really map to file pointers very well, and the IO streams associated with files are really fudges around the native concept of file IO. RandomAccessFile objects much more closely map to the idea of a native OS file pointer - and as such you can seek, read and write.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top