Truncate a file in Java
This post will discuss how to truncate a file to size zero in Java.
There are several ways to truncate a file to size zero before writing in plain Java and using third-party libraries. These are discussed below in detail:
1. Using PrintWriter
We can create a new PrintWriter instance with the specified file. If the file exists, it will be truncated to size zero; otherwise, a new file will be created.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.io.File; import java.io.IOException; import java.io.PrintWriter; class Main { public static void main(String[] args) { File file = new File("foo.out"); try (PrintWriter pw = new PrintWriter(file)) {} catch (IOException e) { e.printStackTrace(); } } } |
2. Using Files Class
With Java 7, we can use Files.newBufferedWriter(…), which returns a BufferedWriter. It opens the file for writing, creating the file if it doesn’t exist, or initially truncating an existing regular-file to a size of 0 if it exists.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; class Main { public static void main(String[] args) { Path path = Paths.get("foo.out"); try (BufferedWriter bw = Files.newBufferedWriter(path)) { // ... } catch (IOException e) { e.printStackTrace(); } } } |
3. Using FileOutputStream
We can also construct a FileOutputStream with the specified file. If the file exists, then it will be truncated to size 0.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; class Main { public static void main(String[] args) { File file = new File("foo.out"); try (FileOutputStream fos = new FileOutputStream(file)) {} catch (IOException e) { e.printStackTrace(); } } } |
4. Using FileWriter
We can also construct a FileWriter object with the given File object. If the file already exists, then the file will be truncated before writing.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.io.File; import java.io.FileWriter; import java.io.IOException; class Main { public static void main(String[] args) { File file = new File("foo.out"); try (FileWriter fw = new FileWriter(file)) { // ... } catch (IOException e) { e.printStackTrace(); } } } |
5. Using FileChannel
FileWriter is a channel for reading, writing, mapping, and manipulating a file. We can use its open() method to open a file. If the StandardOpenOption.TRUNCATE_EXISTING option is specified, the file will be truncated to a size of 0 if it exists.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.io.IOException; import java.nio.channels.FileChannel; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; class Main { public static void main(String[] args) { Path path = Paths.get("foo.out"); try { FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING) .close(); } catch (IOException e) { e.printStackTrace(); } } } |
If StandardOpenOption.TRUNCATE_EXISTING option is not provided, we can call its truncate() method to truncate this channel’s file to the size 0.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.io.IOException; import java.nio.channels.FileChannel; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; class Main { public static void main(String[] args) { Path path = Paths.get("foo.out"); try (FileChannel fc = FileChannel.open(path, StandardOpenOption.WRITE)) { fc.truncate(0); } catch (IOException e) { e.printStackTrace(); } } } |
6. Using RandomAccessFile
A RandomAccessFile instance supports both reading and writing to a random access file. We can call its setLength() method to set the desired length of this file. To truncate the file, pass 0 as an argument to this method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; class Main { public static void main(String[] args) { File file = new File("foo.out"); try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) { raf.setLength(0); } catch (IOException e) { e.printStackTrace(); } } } |
7. Using Guava Library
Guava’s Files.asCharSink(File, Charset, FileWriteMode) method can be used for writing text data to the given file. When no mode is provided, the file will be truncated before writing, or a new file is created when the target file doesn’t exist.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import com.google.common.io.Files; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; class Main { public static void main(String[] args) { File file = new File("foo.out"); try { Files.asCharSink(file, Charset.defaultCharset()).write(""); } catch (IOException e) { e.printStackTrace(); } } } |
8. Using Apache Commons IO
FileUtils class from Apache Commons IO library has the writeStringToFile(File, String, Charset) method, which writes a string to a file. If the file already exists, then the file will be truncated before writing.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; class Main { public static void main(String[] args) { File file = new File("foo.out"); try { FileUtils.writeStringToFile(file, "", Charset.defaultCharset()); } catch (IOException e) { e.printStackTrace(); } } } |
That’s all about truncating a file in Java.
Also See:
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 :)