Skip to content

Commit 016d8b8

Browse files
authored
KTLN-864 Delete content of a file (#986)
* KTLN-864 Delete content of a file * Test file name change
1 parent 064d102 commit 016d8b8

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

core-kotlin-modules/core-kotlin-io/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,12 @@
1313
<version>1.0.0-SNAPSHOT</version>
1414
</parent>
1515

16+
<dependencies>
17+
<dependency>
18+
<groupId>commons-io</groupId>
19+
<artifactId>commons-io</artifactId>
20+
<version>2.15.1</version>
21+
</dependency>
22+
</dependencies>
23+
1624
</project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.deleteFileContent
2+
3+
import org.apache.commons.io.FileUtils
4+
import java.io.*
5+
import java.nio.channels.FileChannel
6+
import java.nio.file.Files
7+
import java.nio.file.StandardOpenOption
8+
9+
class DeleteFileContent {
10+
fun withWriteText(file: File) {
11+
file.writeText("")
12+
}
13+
14+
fun withFileWriter(file: File) {
15+
FileWriter(file).close()
16+
}
17+
18+
fun withPrintWriter(file: File) {
19+
PrintWriter(file).close()
20+
}
21+
22+
fun withFileOutputStream(file: File) {
23+
FileOutputStream(file).close()
24+
}
25+
26+
fun withBufferedWriter(file: File) {
27+
BufferedWriter(FileWriter(file)).close()
28+
}
29+
30+
fun withRandomAccessFile(file: File) {
31+
RandomAccessFile(file, "rw").setLength(0)
32+
}
33+
34+
fun withFileChannel(file: File) {
35+
FileChannel.open(file.toPath(), StandardOpenOption.WRITE).truncate(0).close()
36+
}
37+
38+
fun withFiles(file: File) {
39+
Files.write(file.toPath(), byteArrayOf(), StandardOpenOption.TRUNCATE_EXISTING)
40+
}
41+
42+
fun withFileUtils(file: File) {
43+
FileUtils.write(file, "", Charsets.UTF_8)
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.baeldung.deleteFileContent
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.junit.jupiter.api.AfterEach
5+
import org.junit.jupiter.api.BeforeEach
6+
import org.junit.jupiter.api.Test
7+
import java.io.File
8+
9+
class DeleteFileContentUnitTest {
10+
private val fileName = "src/test/resources/example.txt"
11+
12+
private val deleteFileContent = DeleteFileContent()
13+
14+
@BeforeEach
15+
fun setUp() {
16+
File(fileName).writeText("Hello, World!")
17+
}
18+
19+
@AfterEach
20+
fun tearDown() {
21+
File(fileName).delete()
22+
}
23+
24+
@Test
25+
fun `Delete content with writeText`() {
26+
val file = File(fileName)
27+
deleteFileContent.withWriteText(file)
28+
29+
assertThat(file.readText()).isEmpty()
30+
}
31+
32+
@Test
33+
fun `Delete content with FileWriter`() {
34+
val file = File(fileName)
35+
deleteFileContent.withFileWriter(file)
36+
37+
assertThat(file.readText()).isEmpty()
38+
}
39+
40+
@Test
41+
fun `Delete content with PrintWriter`() {
42+
val file = File(fileName)
43+
deleteFileContent.withPrintWriter(file)
44+
45+
assertThat(file.readText()).isEmpty()
46+
}
47+
48+
@Test
49+
fun withFileOutputStream() {
50+
val file = File(fileName)
51+
deleteFileContent.withFileOutputStream(file)
52+
53+
assertThat(file.readText()).isEmpty()
54+
}
55+
56+
@Test
57+
fun withBufferedWriter() {
58+
val file = File(fileName)
59+
deleteFileContent.withBufferedWriter(file)
60+
61+
assertThat(file.readText()).isEmpty()
62+
}
63+
64+
@Test
65+
fun withRandomAccessFile() {
66+
val file = File(fileName)
67+
deleteFileContent.withRandomAccessFile(file)
68+
69+
assertThat(file.readText()).isEmpty()
70+
}
71+
72+
@Test
73+
fun withFileChannel() {
74+
val file = File(fileName)
75+
deleteFileContent.withFileChannel(file)
76+
77+
assertThat(file.readText()).isEmpty()
78+
}
79+
80+
@Test
81+
fun withFiles() {
82+
val file = File(fileName)
83+
deleteFileContent.withFiles(file)
84+
85+
assertThat(file.readText()).isEmpty()
86+
}
87+
88+
@Test
89+
fun withFileUtils() {
90+
val file = File(fileName)
91+
deleteFileContent.withFileUtils(file)
92+
93+
assertThat(file.readText()).isEmpty()
94+
}
95+
}

0 commit comments

Comments
 (0)