| Author |
Concept On RandomAccessFile
|
Richard Wilson
Ranch Hand
Joined: Jan 12, 2002
Posts: 57
|
|
When write data to a file using RandomAccessFile starting at the file pointer, 1.does the data after the file pointer will be overwritten? 2.does the file pointer will move forward immediately after the data just written? [ February 19, 2002: Message edited by: Richard Wilson ]
|
Richard Wilson
|
 |
Rob Ross
Bartender
Joined: Jan 07, 2002
Posts: 2205
|
|
1. No, not until you actually write to locations past the current file pointer position 2. Yes, the file pointer is updated as bytes are written.
|
Rob
SCJP 1.4
|
 |
Richard Wilson
Ranch Hand
Joined: Jan 12, 2002
Posts: 57
|
|
Originally posted by Rob Ross: 1. No, not until you actually write to locations past the current file pointer position
In my testing example, when I use RandomAccessFile to write some primitive data to a .txt file which contain some strings, without changing the pos of file pointer,the begining part of the file was just overwritten.
|
 |
Rob Ross
Bartender
Joined: Jan 07, 2002
Posts: 2205
|
|
|
When you create a new RandomAccessFile, the file pointer is at the beginning of the file. When you start writing, of course it's going to start writing at the begining of the file!
|
 |
Richard Wilson
Ranch Hand
Joined: Jan 12, 2002
Posts: 57
|
|
|
Then that's to say things behind file pointer will be overwritten.Am I right,Rob?
|
 |
Rob Ross
Bartender
Joined: Jan 07, 2002
Posts: 2205
|
|
The file pointer just indicates where the next byte will come from if you read(), or where the next byte will get written to if you do a write(). If the file pointer is at position zero (start of file) and I read a int, that's going to read 4 bytes from the file, and the file pointer will now be at position 4. If I then write a char, two bytes will be written starting at position 4. When the write is done, the file pointer will have moved to 6.
|
 |
Richard Wilson
Ranch Hand
Joined: Jan 12, 2002
Posts: 57
|
|
Originally posted by Rob Ross: The file pointer just indicates where the next byte will come from if you read(), or where the next byte will get written to if you do a write(). If the file pointer is at position zero (start of file) and I read a int, that's going to read 4 bytes from the file, and the file pointer will now be at position 4. If I then write a char, two bytes will be written starting at position 4. When the write is done, the file pointer will have moved to 6.
so original position 4 and 5's bytes were overwritten by new char,right? [ February 20, 2002: Message edited by: Richard Wilson ]
|
 |
Rob Ross
Bartender
Joined: Jan 07, 2002
Posts: 2205
|
|
|
Yes.
|
 |
 |
|
|
subject: Concept On RandomAccessFile
|
|
|