Delete contents of a file without deleting itself in Java
This post will discuss how to delete the contents of a file without deleting the file itself in Java.
1. Using BufferedWriter
A simple solution is to get a BufferedWriter with the Files.newBufferedWriter(…) method with the TRUNCATE_EXISTING standard open option. It truncates the file to length 0 if it already exists (when opened for writing).
The complete usage is demonstrated below using try-with-resource statement (Java 7+), which automatically take care of closing the opened streams and channels:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; public class Main { public static void main(String[] args) { String fileName = "/user/data/sys.log"; try (BufferedWriter bf = Files.newBufferedWriter(Path.of(fileName), StandardOpenOption.TRUNCATE_EXISTING)) { } catch (IOException e) { e.printStackTrace(); } } } |
2. Using PrintWriter
Alternatively, you can create a new PrintWriter with the specified file name. It results in a file truncated to size zero if it already exists.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.io.IOException; import java.io.PrintWriter; public class Main { public static void main(String[] args) { String fileName = "/user/data/sys.log"; try { PrintWriter pw = new PrintWriter(fileName); pw.close(); } catch (IOException e) { e.printStackTrace(); } } } |
Here’s an equivalent version with try-with-resource statement, which take care of closing the stream:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.io.IOException; import java.io.PrintWriter; public class Main { public static void main(String[] args) { String fileName = "/user/data/sys.log"; try (PrintWriter pw = new PrintWriter(fileName)){ } catch (IOException e) { e.printStackTrace(); } } } |
3. Using FileChannel
The FileChannel class provides the truncate() method that can truncate a file to the given size. Its usage is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.io.IOException; import java.nio.channels.FileChannel; import java.nio.file.Path; import java.nio.file.StandardOpenOption; public class Main { public static void main(String[] args) { String fileName = "/user/data/sys.log"; try { FileChannel.open(Path.of(fileName), StandardOpenOption.WRITE) .truncate(0).close(); } catch (IOException e) { e.printStackTrace(); } } } |
4. Using RandomAccessFile
With the RandomAccessFile class, you can set the length of a file using the setLength() method. To truncate the file, pass the length of the file as 0, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.io.IOException; import java.io.RandomAccessFile; public class Main { public static void main(String[] args) { String fileName = "/user/data/sys.log"; try (RandomAccessFile raf = new RandomAccessFile(fileName, "rw")) { raf.setLength(0); } catch (IOException e) { e.printStackTrace(); } } } |
5. Using FileOutputStream
Finally, you can use FileOutputStream to truncate a file to length 0 if it exists. A typical invocation for this class would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.io.FileOutputStream; import java.io.IOException; public class Main { public static void main(String[] args) { String fileName = "/user/data/sys.log"; try (FileOutputStream fos = new FileOutputStream(fileName, false)) { } catch (IOException e) { e.printStackTrace(); } } } |
That’s all about deleting the contents of a file without deleting the file itself in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)