[utils][K/N] Added some util functions to File
This commit is contained in:
@@ -36,7 +36,7 @@ data class File constructor(internal val javaPath: Path) {
|
|||||||
get() = File(canonicalPath)
|
get() = File(canonicalPath)
|
||||||
|
|
||||||
val name: String
|
val name: String
|
||||||
get() = javaPath.fileName.toString().removeSuffixIfPresent("/") // https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8153248
|
get() = javaPath.fileName.toString().removeSuffixIfPresent(separator) // https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8153248
|
||||||
val extension: String
|
val extension: String
|
||||||
get() = name.substringAfterLast('.', "")
|
get() = name.substringAfterLast('.', "")
|
||||||
val parent: String
|
val parent: String
|
||||||
@@ -70,6 +70,8 @@ data class File constructor(internal val javaPath: Path) {
|
|||||||
sourcePath.recursiveCopyTo(destPath, resetTimeAttributes = resetTimeAttributes)
|
sourcePath.recursiveCopyTo(destPath, resetTimeAttributes = resetTimeAttributes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun renameTo(destination: File) = javaPath.toFile().renameTo(destination.javaPath.toFile())
|
||||||
|
|
||||||
fun mkdirs() = Files.createDirectories(javaPath)
|
fun mkdirs() = Files.createDirectories(javaPath)
|
||||||
fun delete() = Files.deleteIfExists(javaPath)
|
fun delete() = Files.deleteIfExists(javaPath)
|
||||||
fun deleteRecursively() = postorder{Files.delete(it)}
|
fun deleteRecursively() = postorder{Files.delete(it)}
|
||||||
@@ -119,11 +121,13 @@ data class File constructor(internal val javaPath: Path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun deleteOnExit(): File {
|
fun deleteOnExit(): File {
|
||||||
// Works only on the default file system,
|
// Works only on the default file system,
|
||||||
// but that's okay for now.
|
// but that's okay for now.
|
||||||
javaPath.toFile().deleteOnExit()
|
javaPath.toFile().deleteOnExit()
|
||||||
return this // Allow streaming.
|
return this // Allow streaming.
|
||||||
}
|
}
|
||||||
|
fun createNew() = javaPath.toFile().createNewFile()
|
||||||
|
|
||||||
fun readBytes() = Files.readAllBytes(javaPath)
|
fun readBytes() = Files.readAllBytes(javaPath)
|
||||||
fun writeBytes(bytes: ByteArray) = Files.write(javaPath, bytes)
|
fun writeBytes(bytes: ByteArray) = Files.write(javaPath, bytes)
|
||||||
fun appendBytes(bytes: ByteArray)
|
fun appendBytes(bytes: ByteArray)
|
||||||
@@ -135,6 +139,12 @@ data class File constructor(internal val javaPath: Path) {
|
|||||||
|
|
||||||
fun writeText(text: String): Unit = writeLines(listOf(text))
|
fun writeText(text: String): Unit = writeLines(listOf(text))
|
||||||
|
|
||||||
|
fun appendLines(lines: Iterable<String>) {
|
||||||
|
Files.write(javaPath, lines, StandardOpenOption.APPEND)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun appendText(text: String): Unit = appendLines(listOf(text))
|
||||||
|
|
||||||
fun forEachLine(action: (String) -> Unit) {
|
fun forEachLine(action: (String) -> Unit) {
|
||||||
Files.lines(javaPath).use { lines ->
|
Files.lines(javaPath).use { lines ->
|
||||||
lines.forEach { action(it) }
|
lines.forEach { action(it) }
|
||||||
@@ -167,6 +177,7 @@ data class File constructor(internal val javaPath: Path) {
|
|||||||
get() = File(System.getProperty("java.home"))
|
get() = File(System.getProperty("java.home"))
|
||||||
val pathSeparator = java.io.File.pathSeparator
|
val pathSeparator = java.io.File.pathSeparator
|
||||||
val separator = java.io.File.separator
|
val separator = java.io.File.separator
|
||||||
|
val separatorChar = java.io.File.separatorChar
|
||||||
}
|
}
|
||||||
|
|
||||||
fun readStrings() = mutableListOf<String>().also { list -> forEachLine{list.add(it)}}
|
fun readStrings() = mutableListOf<String>().also { list -> forEachLine{list.add(it)}}
|
||||||
|
|||||||
Reference in New Issue
Block a user