Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Issue while erasing text file content  RSS feed

 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • 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.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Posts: 21047
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To delete all contents only, create a FileOutputStream (not using append mode) and then close it immediately.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!