Truncate a File in Kotlin
This post will discuss how to truncate a file to size zero in Kotlin.
1. Using PrintWriter
The recommended solution is to create a PrintWriter instance, which results in the specified file being truncated to size zero. Note that if the file doesn’t exist, a new file will be created.
This is demonstrated below with the use function, which automatically takes care of closing the stream:
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.io.IOException import java.io.PrintWriter fun main() { val fileName = "/user/data/sys.log" try { PrintWriter(fileName).use { } } catch (e: IOException) { e.printStackTrace() } } |
2. Using BufferedWriter
Another option is to create a BufferedWriter using Files.newBufferedWriter(…) and specify the StandardOpenOption.TRUNCATE_EXISTING option to truncate the file to length 0 (if it exists). Its usage is demonstrated below with the use function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.io.IOException import java.nio.file.Files import java.nio.file.Path import java.nio.file.StandardOpenOption fun main() { val fileName = "/user/data/sys.log" try { Files.newBufferedWriter(Path.of(fileName), StandardOpenOption.TRUNCATE_EXISTING ).use { } } catch (e: IOException) { e.printStackTrace() } } |
3. Using FileChannel
The FileChannel class contains the truncate() function to truncate a file to the specified size. It can be used as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.io.IOException import java.nio.channels.FileChannel import java.nio.file.Path import java.nio.file.StandardOpenOption fun main() { val fileName = "/user/data/sys.log" try { FileChannel.open(Path.of(fileName), StandardOpenOption.WRITE) .truncate(0).close() } catch (e: IOException) { e.printStackTrace() } } |
Alternately, you can directly specify the StandardOpenOption.TRUNCATE_EXISTING option to the open() function, which results in the file being truncated to a size of 0 (if it exists).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.io.IOException import java.nio.channels.FileChannel import java.nio.file.Paths import java.nio.file.StandardOpenOption fun main() { val path = Paths.get("foo.out") try { FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING) .close() } catch (e: IOException) { e.printStackTrace() } } |
4. Using RandomAccessFile
The RandomAccessFile class provides support for setting the length of a file using the setLength() function. To truncate a file, you can open the file in rw mode and pass the length as 0 to the setLength() function:
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.io.IOException import java.io.RandomAccessFile fun main() { val fileName = "/user/data/sys.log" try { RandomAccessFile(fileName, "rw").use { it.setLength(0) } } catch (e: IOException) { e.printStackTrace() } } |
5. Using FileOutputStream
The following solution constructs a FileOutputStream instance with append mode as false, which truncates the file to length 0. Here’s how code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.io.FileOutputStream import java.io.IOException fun main() { val fileName = "/user/data/sys.log" try { FileOutputStream(fileName, false).use { } } catch (e: IOException) { e.printStackTrace() } } |
6. Using FileWriter
Finally, you can also create a FileWriter object which would truncate the file if it already exists. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.io.File import java.io.FileWriter import java.io.IOException fun main() { val file = File("foo.out") try { FileWriter(file).use { } } catch (e: IOException) { e.printStackTrace() } } |
That’s all about truncating a file in Kotlin.
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 :)