Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2008

Tip: Looking for answers? Try searching our database.

Deleting contents of file

Thread view: 
christopher_board@yahoo.co.uk - 28 Feb 2008 12:12 GMT
Hi all,

I am currently devloping a java application where the application when
it is launched reads in a CSV file and then populates that data into a
JList. I would like the user to be able to select an item from the
JList and then click a button which deletes the row that item is in
within the CSV file. How would I go about doing this.

Any help in this matter would be highly appreciated.

Thank you
Gordon Beaton - 28 Feb 2008 12:07 GMT
> I am currently devloping a java application where the application
> when it is launched reads in a CSV file and then populates that data
[quoted text clipped - 3 lines]
>
> Any help in this matter would be highly appreciated.

Files are much like large arrays of bytes. You can't delete a line
without rewriting the entire file contents from the start of the
deletion to the end of the file. You might as well just rewrite the
entire file except the deleted entry.

When the user deletes the item from the JList, write the remaining
items to a new (temporary) file, delete the old file, then rename the
temporary file. By doing it this way you can avoid deleting the
original file if any errors occur while writing the temporary one.

/gordon

--
Mark Space - 28 Feb 2008 18:23 GMT
> When the user deletes the item from the JList, write the remaining
> items to a new (temporary) file, delete the old file, then rename the
> temporary file. By doing it this way you can avoid deleting the
> original file if any errors occur while writing the temporary one.

Rather than deleting the old file, consider moving to a back-up file.
Like filename + ".bak" or "~"+filename.  That way, if you fugger it up,
you still have the backup. ;)
John B. Matthews - 02 Mar 2008 01:51 GMT
> > When the user deletes the item from the JList, write the remaining
> > items to a new (temporary) file, delete the old file, then rename the
[quoted text clipped - 4 lines]
> Like filename + ".bak" or "~"+filename.  That way, if you fugger it up,
> you still have the backup. ;)

Excellent advice. Also consider using one of the createTempFile()
methods in java.io.File. They're platform agnostic and generate a unique
name with each invocation.

Signature

John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews

Roedy Green - 29 Feb 2008 03:05 GMT
On Thu, 28 Feb 2008 04:12:26 -0800 (PST),
christopher_board@yahoo.co.uk wrote, quoted or indirectly quoted
someone who said :

>I am currently devloping a java application where the application when
>it is launched reads in a CSV file and then populates that data into a
>JList. I would like the user to be able to select an item from the
>JList and then click a button which deletes the row that item is in
>within the CSV file. How would I go about doing this.

the only way to delete a string in a file is to copy the file up to
the slice, skip over the slice, and write the rest.

You can also chop a file, cutting off the tail.

See http://mindprod.com/jgloss/file.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Gordon Beaton - 29 Feb 2008 06:41 GMT
> the only way to delete a string in a file is to copy the file up to
> the slice, skip over the slice, and write the rest.

It isn't necessary to copy the first part of the file. It's only
necessary to rewrite from the cut to the end.

/gordon

--
John W. Kennedy - 29 Feb 2008 19:05 GMT
>> the only way to delete a string in a file is to copy the file up to
>> the slice, skip over the slice, and write the rest.
>
> It isn't necessary to copy the first part of the file. It's only
> necessary to rewrite from the cut to the end.

If the file is of any size, this can be very time-consuming, as you'll
be jittering the access arm one record at a time. Making a new file is
usually much faster, in practice, as the physical I/O can be done in
bursts. It is also recoverable, in the event of a crash.

Other solutions:
   Use a database table, and delete the row.
   Use some kind of indexed file, and delete the record.
   Read the entire file into memory, manipulate it there, and rewrite.
Signature

John W. Kennedy
"Sweet, was Christ crucified to create this chat?"
  -- Charles Williams.  "Judgement at Chelmsford"

Roedy Green - 01 Mar 2008 19:17 GMT
On Fri, 29 Feb 2008 14:05:59 -0500, "John W. Kennedy"
<jwkenne@attglobal.net> wrote, quoted or indirectly quoted someone who
said :

>If the file is of any size, this can be very time-consuming, as you'll
>be jittering the access arm one record at a time. Making a new file is
>usually much faster, in practice, as the physical I/O can be done in
>bursts.

with the same size chunk buffer, it would be the same speed, unless
you had two different physical disks. You have the same arm jerking
back and forth.

The optimal size of the buffer is probably a lot bigger than the 1-8K
typically used in the olden days.  I wish there were a way choosing
that size were automated, and customised to the particular machine.
It is silly for a machine with a 20 gigs of ram to fiddle around with
4K buffers.

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roedy Green - 01 Mar 2008 19:14 GMT
>It isn't necessary to copy the first part of the file. It's only
>necessary to rewrite from the cut to the end.

I lived on an island for a number of years where power would
go out very frequently. The battery backup did not always rescue you.
So my tendency is to avoid doing anything that if interrupted in mid
flight would destroy your file, like inplace file updates.  I like to
create the new file, and only once it is ready, do the delete/rename.
That leaves only a tiny window of vulnerability.

Back in DOS days, when disks were tiny, but hairy-chested,  I did a
lot of in place updating, including creating holes, and deleting.  You
have to be careful how you do your slides, working bottom up or top
down to avoid overwriting.  The same applies to in-ram sliding.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roedy Green - 29 Feb 2008 03:06 GMT
On Thu, 28 Feb 2008 04:12:26 -0800 (PST),
christopher_board@yahoo.co.uk wrote, quoted or indirectly quoted
someone who said :

>I am currently devloping a java application where the application when
>it is launched reads in a CSV file and then populates that data into a
>JList. I would like the user to be able to select an item from the
>JList and then click a button which deletes the row that item is in
>within the CSV file. How would I go about doing this.

Another approach is to read the whole file into RAM as a String.
Fiddle it as you please.  Write out the entire file to a temp. When it
succeeds, delete the original and rename the temp to the original.

For sample code see http://mindprod.com/products1.html#COMPACTOR

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.