Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to delete the content of text file without deleting itself

Tags:

java

android

I want to copy the content of file 'A' to file 'B'. after the copying is done I want to clear the content of file 'A' and want to write on it from its beginning. I can't delete file 'A' as it is related to some other task.

I was able to copy the content using java's file API(readLine() ), but don't know how to clear the content of file and set the file pointer to the beginning of the file.

like image 781
brig Avatar asked Aug 09 '11 10:08

brig


People also ask

How do you delete text in a file?

Open the file with your text editor and press End. Highlight and PgUp to delete the remaining bytes that don't belong (usually recognizable by ASCII garbage characters).


2 Answers

Just print an empty string into the file:

PrintWriter writer = new PrintWriter(file); writer.print(""); writer.close(); 
like image 141
Vlad Avatar answered Sep 25 '22 06:09

Vlad


I don't believe you even have to write an empty string to the file.

PrintWriter pw = new PrintWriter("filepath.txt"); pw.close(); 
like image 31
camleng Avatar answered Sep 25 '22 06:09

camleng