• Post Reply Bookmark Topic Watch Topic
  • New Topic

Empty the File  RSS feed

 
Piyush Walia
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible that i can completly remove the Content of a given '.txt'
File.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you open it for writing, write a single character, and close it (without specifying 'append-mode'), the file will only contain one character.
How many characters will it contain if we write zero characters? I guess zero.
Did you try this?
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, use methods delete() and createNewFile() from the File class.

--Tim
 
Ramesh Aiyappan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can empty the file using a new FileOutputStream().The following will empty the contents of the file temp.txt.
File f = new File("C:\\temp.txt");
new FileOutputStream(f);
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!