aspose file tools*
The moose likes Java in General and the fly likes insertion and deletion of record from a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "insertion and deletion of record from a text file" Watch "insertion and deletion of record from a text file" New topic
Author

insertion and deletion of record from a text file

Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
Hi, I would like to know how to delete a record from a text file and delete it

The code below only inserts it in the memory but not in the text file





Textfile..

0|S8470017G|Abhradeep Banerjee | Singapore |09/07/2007 | 13:28 | Kolkata, India | 09/01/2010 | 16:28
1|S22209619B|Debabrata Banerjee | Kolkata, India |09/08/2007 | 13:28 | Singapore| 09/01/2010 | 16:28
2|S8328944B|Debabrata Banerjee | Kolkata, India |09/08/2007 | 13:28 | Singapore | 09/01/2010 | 16:28
3|S8470017G|Abhradeep Banerjee | Singapore |09/07/2007 | 13:28 | Kolkata, India | 09/07/2007 | 16:28
4|S8470017G|Abhradeep Banerjee | Kolkata, India |09/08/2007 | 13:28 | Singapore| 09/08/2007 | 16:28

Thanks

Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

If I read your code correct then you have the text file read into

in which you read/add/delete records - isn't that correct??

If I'm right, then you just have to 'empty' the LinkedList back into Travel_Records.txt - override it.

So your task is to find a way to write a file - you now know how to use the BufferedReader - maybe there also is a BufferedWriter


Regards, Rene Larsen
Dropbox Invite
Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
when i tried bufferedWriter the whole file went blank
ill try and do it again tommorow and post it
Thanks for your quick response
Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
The insert record doesnt seem to work it deletes of all the records for some reason

mainmethod





// Travel_Records.txt

0|S8470017G|Abhradeep Banerjee | Singapore |09/07/2007 | 13:28 | Kolkata, India | 09/01/2010 | 16:28
1|S22209619B|Debabrata Banerjee | Kolkata, India |09/08/2007 | 13:28 | Singapore| 09/01/2010 | 16:28
2|S8328944B|Debabrata Banerjee | Kolkata, India |09/08/2007 | 13:28 | Singapore | 09/01/2010 | 16:28
3|S8470017G|Abhradeep Banerjee | Singapore |09/07/2007 | 13:28 | Kolkata, India | 09/07/2007 | 16:28
4|S8470017G|Abhradeep Banerjee | Kolkata, India |09/08/2007 | 13:28 | Singapore| 09/08/2007 | 16:28
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

What does LinkedList<Travel> LL contain ??

Does it not include the whole new text file (the modified version) ??

Yeah, maybe the BufferedWriter isn't the best solution - try only to use "new PrintWriter(new FileWriter(path, true));" - where the last parameter stands for 'append' - then you can write to the file, after it has been closed, and the append context to the file - but you are not able to 'control' what already has been written (or what the file already contains)

When you use a writer without the 'append' flag, it will start with an empty file all the time.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

Using foreach loops is a lot cleaner. For example:


SCJP 6 || SCWCD 5
Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
ooo ok

i never used for each before thanks ill try and figure out how to use it properly
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
And don't forget to close your output stream
Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
ok i got the insert working now just need to get the delete one working
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: insertion and deletion of record from a text file