how to delete the content of text file without deleting itself
Just print an empty string into the file:
PrintWriter writer = new PrintWriter(file);
writer.print("");
writer.close();
Java - How to Clear a text file without deleting it?
If you want to clear the file without deleting may be you can workaround this
public static void clearTheFile() {
FileWriter fwOb = new FileWriter("FileName", false);
PrintWriter pwOb = new PrintWriter(fwOb, false);
pwOb.flush();
pwOb.close();
fwOb.close();
}
Edit: It throws exception so need to catch the exceptions
Clearing a text file without deleting it
You can start by overwriting the file with an empty string, and then append your data afterwards. You could use this to overwrite the file:
System.IO.File.WriteAllText(Path, "")
The benefit of this as opposed to deleting and recreating the file is that you will preserve the original create date of the file along with any other metadata.
How to delete all text in a text file on ftp server without deleting file itself in vb.net
The way to go should be upload a new empty file overwriting the one on the FTP, with the same filename and path.
Dim wcFTP As New WebClient()
'wcFTP.Proxy = ...
wcFTP.BaseAddress = "ftp://foobar.domain.com"
wcFTP.Credentials = New NetworkCredential("user", "pass")
Dim sFilePath As String '= ...
Dim sFtpPath As String = wcFTP.BaseAddress & sFolder & Path.GetFileName(sFilePath)
wcFTP.UploadFile(sFtpPath, sFilePath)
Where sFilePath
is the path of the empty file with the same name as the one in the server.
To get an empty file you can donwload and empty the file on the ftp, or simply create a new file with the same name and no data. For example:
Dim sFilePath As String = "C:\Folder\fileName.xml"
File.Create(sFilePath).Dispose()
Howto clear a textfile without deleting file?
String path = "c:/file.ini";
using (var stream = new FileStream(path, FileMode.Truncate))
{
using (var writer = new StreamWriter(stream))
{
writer.Write("data");
}
}
How to delete the contents of a file using the FileOutputStream class in Java?
If you want to delete the contents of the file, but not the file itself, you could do:
PrintWriter pw = new PrintWriter("file.txt");
pw.close();
A few seconds of Googling got me this:
how to delete the content of text file without deleting itself
How to clear a text file without deleting it?
To delete the file completely, do:
File file = new File("file.txt");
f.delete();
Related Topics
Default Value in Lombok. How to Init Default With Both Constructor and Builder
Spring Boot JPA - Onetomany Relationship Causes Infinite Loop
How to Test Class Which Implements Runnable With Junit
How to Get Response as Json With Responseentity in Java
Parsing the Nested Json Array Using Jackson Library in Java
Can a @Manytoone JPA Relation Be Null
Classcastexception: Java.Math.Biginteger Cannot Be Cast to Java.Lang.Long on Connect to MySQL
Run Gradle Task With Spring Profiles (Integration Tests)
How to Add Multiple Components to a Jframe
How to Exit an Android App Programmatically
How to Change the Background Color of a Textfield Without Changing the Border in Javafx
Gradle Java9 Could Not Target Platform: 'Java Se 9' Using Tool Chain: 'Jdk 8 (1.8)'
Opening Intent Crashing the App Android Studio
Cannot Get Jedis Connection; Could Not Get a Resource from the Pool
How to Get My Loops to Display Horizontally Instead of Vertically