Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Concept On RandomAccessFile

 
Richard Wilson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Richard Wilson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then that's to say things behind file pointer will be overwritten.Am I right,Rob?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic