• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with random access file  RSS feed

 
rachana ravali
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to append some content to each line in a text file.
But when i use random access file and write contents to the file.. the content in next are getting over writed.
Is it not possible to write in the middle of a file using random access file without getting the contents next to it overwritten>

i have a example program like this :

File file = new File("C:\\Desktop\\RandomAccessFileExample.txt");
RandomAccessFile raf = new RandomAccessFile(file, "rw");


raf.seek(4);

raf.writeBytes("This will complete the example");

raf.close();

} catch (IOException e) {

System.out.println("IOException:");
e.printStackTrace();

}

}

and my RandomAccessFileExample.txt has the contents like
ser1
ser2

If i want insert some contents after ser1 and before ser2 using the above program...
the ser2 in the text is overwritten with the new contents.

How to insert contents in the middle of a file ?
Please help me.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never seen any way of inserting in the middle of a file. You might have to read what you have and write it back afterwards.

Anybody else?

[edit]Remove comments about moving thread.[/edit]
[ November 05, 2008: Message edited by: Campbell Ritchie ]
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
I have never seen any way of inserting in the middle of a file.
No, that's because there isn't any way. A file is just an array of bytes on a disk. Creating a new, modified, file is the way to go.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Paul. I suspected as much, but wasn't quite sure.
 
rachana ravali
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but in the specification it is written as we can read and write any where in the file unlike other file objects???
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It goes on about cursors and byte[] arrays in the API; it doesn't suggest (at least the way I read it) that you can insert anywhere.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rachana ravali:
but in the specification it is written as we can read and write any where in the file unlike other file objects???

Yes, that's true. You can read and write anywhere in the file. Reading reads bytes that are in the file. Writing overwrites bytes that are in the file.
 
rachana ravali
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to both of you ... Paul and Campbell
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!