Kotlin Standard Library JDK 7 extension providing Path utilities and platform-specific implementations for Java NIO and JDK 7+ features
npx @tessl/cli install tessl/maven-org-jetbrains-kotlin--kotlin-stdlib-jdk7@2.2.0The Kotlin Standard Library JDK 7 extension provides comprehensive Path utilities using Java NIO APIs introduced in JDK 7 and higher. It offers a complete set of extension functions for java.nio.file.Path that enable idiomatic Kotlin file system operations, I/O, directory traversal, and more.
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:2.2.0'Note: The Java 9 module kotlin.stdlib.jdk7 is deprecated and empty. For Java 9+ module systems, use kotlin.stdlib instead which transitively includes this functionality.
import kotlin.io.path.*
import java.nio.file.Path
import java.nio.file.PathsFor specific functionality:
import kotlin.io.path.exists
import kotlin.io.path.createDirectories
import kotlin.io.path.readText
import kotlin.io.path.writeText
import kotlin.io.path.copyToRecursively
import kotlin.io.path.walkimport kotlin.io.path.*
import java.nio.file.Paths
// Create and manipulate paths
val path = Paths.get("/home/user/documents/file.txt")
val filename = path.name // "file.txt"
val extension = path.extension // "txt"
val parent = path.parent // Path to "/home/user/documents"
// Check file existence and properties
if (path.exists()) {
println("File size: ${path.fileSize()} bytes")
println("Is readable: ${path.isReadable()}")
println("Is directory: ${path.isDirectory()}")
}
// Create directories and files
val newDir = Paths.get("/tmp/myapp")
newDir.createDirectories()
val configFile = newDir / "config.txt"
configFile.writeText("Setting=Value")
// Read and write files
val content = configFile.readText()
println("Config content: $content")
// List directory contents
newDir.listDirectoryEntries("*.txt").forEach { file ->
println("Found text file: ${file.name}")
}The kotlin-stdlib-jdk7 extension is built around several key components:
java.nio.file.Path providing idiomatic Kotlin operationsCore path manipulation including resolution, relativization, and property access for robust file system navigation.
val Path.name: String
val Path.nameWithoutExtension: String
val Path.extension: String
val Path.pathString: String
val Path.invariantSeparatorsPathString: String
fun Path.absolute(): Path
fun Path.relativeTo(base: Path): Path
operator fun Path.div(other: Path): Path
operator fun Path.div(other: String): Path
fun Path(path: String): Path
fun Path(base: String, vararg subpaths: String): Path
fun URI.toPath(): PathFile and directory operations including existence checks, creation, deletion, copying, and attribute management.
fun Path.exists(vararg options: LinkOption): Boolean
fun Path.isDirectory(vararg options: LinkOption): Boolean
fun Path.isRegularFile(vararg options: LinkOption): Boolean
fun Path.createFile(vararg attributes: FileAttribute<*>): Path
fun Path.createDirectory(vararg attributes: FileAttribute<*>): Path
fun Path.createDirectories(vararg attributes: FileAttribute<*>): Path
fun Path.copyTo(target: Path, overwrite: Boolean = false): Path
fun Path.moveTo(target: Path, overwrite: Boolean = false): Path
fun Path.deleteExisting()
fun Path.deleteIfExists(): Boolean
fun Path.listDirectoryEntries(glob: String = "*"): List<Path>Comprehensive file input/output operations including streams, readers/writers, and convenience methods for text and binary data.
fun Path.inputStream(vararg options: OpenOption): InputStream
fun Path.outputStream(vararg options: OpenOption): OutputStream
fun Path.reader(charset: Charset = Charsets.UTF_8, vararg options: OpenOption): InputStreamReader
fun Path.bufferedReader(charset: Charset = Charsets.UTF_8, bufferSize: Int = DEFAULT_BUFFER_SIZE, vararg options: OpenOption): BufferedReader
fun Path.readText(charset: Charset = Charsets.UTF_8): String
fun Path.writeText(text: CharSequence, charset: Charset = Charsets.UTF_8, vararg options: OpenOption)
fun Path.readBytes(): ByteArray
fun Path.writeBytes(array: ByteArray, vararg options: OpenOption)
fun Path.readLines(charset: Charset = Charsets.UTF_8): List<String>
fun Path.writeLines(lines: Iterable<CharSequence>, charset: Charset = Charsets.UTF_8, vararg options: OpenOption): PathAdvanced directory tree traversal with walking sequences and customizable file visitor patterns for complex file operations.
fun Path.walk(vararg options: PathWalkOption): Sequence<Path>
fun Path.visitFileTree(visitor: FileVisitor<Path>, maxDepth: Int = Int.MAX_VALUE, followLinks: Boolean = false)
fun Path.visitFileTree(maxDepth: Int = Int.MAX_VALUE, followLinks: Boolean = false, builderAction: FileVisitorBuilder.() -> Unit)
fun fileVisitor(builderAction: FileVisitorBuilder.() -> Unit): FileVisitor<Path>
enum class PathWalkOption {
INCLUDE_DIRECTORIES,
BREADTH_FIRST,
FOLLOW_LINKS
}Experimental APIs for recursive file operations with comprehensive error handling and customizable behaviors.
@ExperimentalPathApi
fun Path.copyToRecursively(
target: Path,
onError: (source: Path, target: Path, exception: Exception) -> OnErrorResult = { _, _, exception -> throw exception },
followLinks: Boolean,
overwrite: Boolean
): Path
@ExperimentalPathApi
fun Path.deleteRecursively()@ExperimentalPathApi
annotation class ExperimentalPathApi
@SinceKotlin("2.1")
enum class PathWalkOption {
INCLUDE_DIRECTORIES,
BREADTH_FIRST,
FOLLOW_LINKS
}
@ExperimentalPathApi
enum class OnErrorResult {
SKIP_SUBTREE,
TERMINATE
}
@ExperimentalPathApi
enum class CopyActionResult {
CONTINUE,
SKIP_SUBTREE,
TERMINATE
}
@ExperimentalPathApi
interface CopyActionContext {
fun Path.copyToIgnoringExistingDirectory(target: Path, followLinks: Boolean): CopyActionResult
}
@SinceKotlin("2.1")
interface FileVisitorBuilder {
fun onPreVisitDirectory(function: (directory: Path, attributes: BasicFileAttributes) -> FileVisitResult)
fun onVisitFile(function: (file: Path, attributes: BasicFileAttributes) -> FileVisitResult)
fun onVisitFileFailed(function: (file: Path, exception: IOException) -> FileVisitResult)
fun onPostVisitDirectory(function: (directory: Path, exception: IOException?) -> FileVisitResult)
}