• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Remove a line from a CSV file - Open CSV

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing a web service that reads from a csv file and for the DELETE operation remove a specific line. I have the following method, but I cannot actually remove the line and write back into the csv:



Can you help? I also have some strange behaviour whereby the csv gets completely wiped out. Am I right in closing the reader before writing back (with the append option set to true) into the file?

Thanks
 
Saloon Keeper
Posts: 9720
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot read and write to the same file. Read from the original file and write to a new file, then delete the original file, then rename the new file to the name the original file had.
 
Geane Norm
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You cannot read and write to the same file. Read from the original file and write to a new file, then delete the original file, then rename the new file to the name the original file had.



Thanks, I have tried the following:



All good as it creates a new file without that particular product (when I comment out both the delete and rename methods), however when the 2 utility private methods are included if fails when renaming it and it results in the old file being totally wiped out (and the new not being created at all). Where am I going wrong? Can you help?
 
Geane Norm
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Geane Norm wrote:

Carey Brown wrote:You cannot read and write to the same file. Read from the original file and write to a new file, then delete the original file, then rename the new file to the name the original file had.



Thanks, I have tried the following:



All good as it creates a new file without that particular product (when I comment out both the delete and rename methods), however when the 2 utility private methods are included if fails when renaming it and it results in the old file being totally wiped out (and the new not being created at all). Where am I going wrong? Can you help?



I actually realised that the service method in my spring boot had some issue. All fixed!
 
Carey Brown
Saloon Keeper
Posts: 9720
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have deleteFile() return true if delete was successful and only then call renameFile().
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic