Skip to content

Commit 56e89f6

Browse files
[KTLN-708] Add Samples (#762)
* [KTLN-708] Add Samples * [KTLN-708] Add Samples * [KTLN-708] Add Samples * [KTLN-708] Add Samples * Update core-kotlin-modules/core-kotlin-files/src/test/kotlin/com/baeldung/deletefile/DeleteFileUnitTest.kt Co-authored-by: KevinGilmore <[email protected]> * Update core-kotlin-modules/core-kotlin-files/src/test/kotlin/com/baeldung/deletefile/DeleteFileUnitTest.kt --------- Co-authored-by: KevinGilmore <[email protected]>
1 parent f412357 commit 56e89f6

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.deletefile
2+
3+
import java.io.File
4+
import java.io.IOException
5+
6+
fun deleteFile(filePath: String) {
7+
val file = File(filePath)
8+
if (file.exists() && file.isFile) {
9+
file.delete()
10+
}
11+
}
12+
13+
fun deleteDirectory(directory: File) {
14+
if (directory.exists() && directory.isDirectory) {
15+
directory.listFiles()?.forEach { file ->
16+
if (file.isDirectory) {
17+
deleteDirectory(file)
18+
} else {
19+
file.delete()
20+
}
21+
}
22+
directory.delete()
23+
}
24+
}
25+
26+
fun safeDeleteDirectory(directory: File) {
27+
try {
28+
deleteDirectory(directory)
29+
} catch (e: IOException) {
30+
e.printStackTrace() // Or handle the exception as needed
31+
}
32+
}
33+
34+
fun File.deleteContentsRecursively(): Boolean {
35+
if (!this.exists()) return false
36+
if (!this.isDirectory) return this.delete()
37+
return this.walkBottomUp().all { it.delete() }
38+
}
39+
40+
val success = File("/path/to/directory").deleteRecursively()
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.deletefile
2+
3+
import org.junit.jupiter.api.Assertions.*
4+
import org.junit.jupiter.api.Test
5+
import java.io.File
6+
7+
class DeleteFileUnitTest {
8+
9+
@Test
10+
fun `given file path when deleteFile called then file is deleted`() {
11+
val tempFile = createTempFile()
12+
assertTrue(tempFile.exists())
13+
14+
deleteFile(tempFile.absolutePath)
15+
16+
assertFalse(tempFile.exists())
17+
}
18+
19+
@Test
20+
fun `given directory when deleteDirectory called then directory and its contents are deleted`() {
21+
val tempDir = createTempDir()
22+
val tempFileInDir = File(tempDir, "tempFile.txt").apply { createNewFile() }
23+
assertTrue(tempDir.exists())
24+
assertTrue(tempFileInDir.exists())
25+
26+
deleteDirectory(tempDir)
27+
28+
assertFalse(tempDir.exists())
29+
assertFalse(tempFileInDir.exists())
30+
}
31+
32+
@Test
33+
fun `given a non-existent file should not throw`() {
34+
val file = File("imaginary-file.txt")
35+
36+
assertDoesNotThrow {
37+
safeDeleteDirectory(file)
38+
}
39+
}
40+
41+
@Test
42+
fun `given directory when deleteDirectory called then directory and its contents are deleted recursively`() {
43+
val tempDir = createTempDir()
44+
val innerTempDir = File(tempDir, "innerTempDir").apply { mkdir() }
45+
val tempFileInDir = File(innerTempDir, "tempFile.txt").apply { createNewFile() }
46+
47+
assertTrue(tempDir.exists())
48+
assertTrue(innerTempDir.exists())
49+
assertTrue(tempFileInDir.exists())
50+
51+
tempDir.deleteContentsRecursively()
52+
53+
assertFalse(tempDir.exists())
54+
assertFalse(innerTempDir.exists())
55+
assertFalse(tempFileInDir.exists())
56+
}
57+
}

0 commit comments

Comments
 (0)