Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Issue while erasing text file content

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am working on a Swing app. In my app I am writing some data to the text files. I want to clear these text files (erase all data in it) in one operation when I again run this app. But I guess I am not able to do so since, these files get open in an read only mode when I run the app next time.
I am currently using following code clear files. Its basically deleting and again creating file with the same name.

File sFile = new File("logCases.txt");
sFile.delete();
File sFile_New = new File("logCases.txt");
sFile_New.createNewFile();

but no new file is getting created .. is there any other work around for this (to erase data in file)? I guess instead of deleting erasing would be a better option.
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out File#createTempFile() and File#deleteOnExit()

This question is not really Swing specific. Moving to a more appropriate forum
 
Sheriff
Posts: 22563
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To delete all contents only, create a FileOutputStream (not using append mode) and then close it immediately.
 
reply
    Bookmark Topic Watch Topic
  • New Topic