How to Delete the Content of Text File Without Deleting Itself

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



Leave a reply



Submit