CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-jetbrains-kotlin--kotlin-stdlib-jdk7

Kotlin Standard Library JDK 7 extension providing Path utilities and platform-specific implementations for Java NIO and JDK 7+ features

Pending
Overview
Eval results
Files

path-operations.mddocs/

Path Operations

Core path manipulation functionality providing string properties, resolution operations, relativization, and factory functions for robust file system navigation.

Capabilities

Path Properties

Access path components as strings with proper handling of edge cases.

/**
 * Returns the name of the file or directory denoted by this path as a string,
 * or an empty string if this path has zero path elements.
 */
val Path.name: String

/**
 * Returns the name of this file or directory without an extension,
 * or an empty string if this path has zero path elements.
 */
val Path.nameWithoutExtension: String

/**
 * Returns the extension of this path (not including the dot),
 * or an empty string if it doesn't have one.
 */
val Path.extension: String

/**
 * Returns the string representation of this path using the default name separator.
 * This property is a synonym to Path.toString() function.
 */
val Path.pathString: String

/**
 * Returns the string representation of this path using the invariant separator '/'
 * to separate names in the path.
 */
val Path.invariantSeparatorsPathString: String

/**
 * @deprecated Use invariantSeparatorsPathString property instead.
 * Returns the string representation of this path using the invariant separator '/'.
 */
@Deprecated("Use invariantSeparatorsPathString property instead.", ReplaceWith("invariantSeparatorsPathString"))
val Path.invariantSeparatorsPath: String

Usage Examples:

import kotlin.io.path.*
import java.nio.file.Paths

val path = Paths.get("/home/user/documents/report.pdf")

println(path.name)                      // "report.pdf"
println(path.nameWithoutExtension)      // "report"
println(path.extension)                 // "pdf"
println(path.pathString)                // "/home/user/documents/report.pdf"
println(path.invariantSeparatorsPathString) // "/home/user/documents/report.pdf" (Unix) or converts \ to / (Windows)

// Edge cases
val root = Paths.get("/")
println(root.name)                      // ""
println(root.extension)                 // ""

Path Resolution

Convert between relative and absolute paths with proper resolution handling.

/**
 * Converts this possibly relative path to an absolute path.
 * If this path is already absolute, returns this path unchanged.
 */
fun Path.absolute(): Path

/**
 * Converts this possibly relative path to an absolute path and returns its string representation.
 * Basically, this method is a combination of calling absolute() and pathString.
 * May throw an exception if the file system is inaccessible or getting the default directory path is prohibited.
 */
fun Path.absolutePathString(): String

Usage Examples:

import kotlin.io.path.*
import java.nio.file.Paths

val relativePath = Paths.get("../config/settings.xml")
val absolutePath = relativePath.absolute()
println("Absolute path: $absolutePath")

// Get absolute path as string in one call
val absoluteString = relativePath.absolutePathString()
println("Absolute string: $absoluteString")

Path Relativization

Calculate relative paths between different locations with error handling for different roots.

/**
 * Calculates the relative path for this path from a base path.
 * Note that the base path is treated as a directory.
 * @throws IllegalArgumentException if this and base paths have different roots.
 */
fun Path.relativeTo(base: Path): Path

/**
 * Calculates the relative path for this path from a base path.
 * @return the relative path from base to this, or this if this and base paths have different roots.
 */
fun Path.relativeToOrSelf(base: Path): Path

/**
 * Calculates the relative path for this path from a base path.
 * @return the relative path from base to this, or null if this and base paths have different roots.
 */
fun Path.relativeToOrNull(base: Path): Path?

Usage Examples:

import kotlin.io.path.*
import java.nio.file.Paths

val basePath = Paths.get("/home/user/projects")
val targetPath = Paths.get("/home/user/projects/myapp/src/main.kt")

// Calculate relative path
val relativePath = targetPath.relativeTo(basePath)
println(relativePath)  // "myapp/src/main.kt"

// Safe variant that returns self if different roots
val safeRelative = targetPath.relativeToOrSelf(basePath)
println(safeRelative)

// Nullable variant
val nullableRelative = targetPath.relativeToOrNull(basePath)
println(nullableRelative ?: "Different roots")

Path Division Operators

Convenient operators for path resolution and joining.

/**
 * Resolves the given other path against this path.
 * This operator is a shortcut for the Path.resolve function.
 */
operator fun Path.div(other: Path): Path

/**
 * Resolves the given other path string against this path.
 * This operator is a shortcut for the Path.resolve function.
 */
operator fun Path.div(other: String): Path

Usage Examples:

import kotlin.io.path.*
import java.nio.file.Paths

val baseDir = Paths.get("/home/user")
val configDir = baseDir / "config"
val configFile = configDir / "app.properties"

println(configFile)  // "/home/user/config/app.properties"

// Combining with other paths
val logsPath = Paths.get("logs")
val fullLogsPath = baseDir / logsPath
println(fullLogsPath)  // "/home/user/logs"

Path Factory Functions

Create Path instances from strings and URIs with proper handling.

/**
 * Converts the provided path string to a Path object of the default filesystem.
 */
fun Path(path: String): Path

/**
 * Converts the name sequence specified with the base path string and a number of subpaths
 * additional names to a Path object of the default filesystem.
 */
fun Path(base: String, vararg subpaths: String): Path

/**
 * Converts this URI to a Path object.
 */
fun URI.toPath(): Path

Usage Examples:

import kotlin.io.path.*
import java.net.URI

// Create paths from strings
val simplePath = Path("/home/user/file.txt")
val composedPath = Path("/home", "user", "documents", "report.pdf")

println(simplePath)   // "/home/user/file.txt"
println(composedPath) // "/home/user/documents/report.pdf"

// Create path from URI
val uri = URI("file:///home/user/file.txt")
val pathFromUri = uri.toPath()
println(pathFromUri)  // "/home/user/file.txt"

Temporary Files and Directories

Create temporary files and directories with optional prefixes, suffixes, and attributes.

/**
 * Creates an empty file in the default temp directory, using
 * the given prefix and suffix to generate its name.
 */
fun createTempFile(prefix: String? = null, suffix: String? = null, vararg attributes: FileAttribute<*>): Path

/**
 * Creates an empty file in the specified directory, using
 * the given prefix and suffix to generate its name.
 */
fun createTempFile(directory: Path?, prefix: String? = null, suffix: String? = null, vararg attributes: FileAttribute<*>): Path

/**
 * Creates a new directory in the default temp directory, using the given prefix to generate its name.
 */
fun createTempDirectory(prefix: String? = null, vararg attributes: FileAttribute<*>): Path

/**
 * Creates a new directory in the specified directory, using the given prefix to generate its name.
 */
fun createTempDirectory(directory: Path?, prefix: String? = null, vararg attributes: FileAttribute<*>): Path

Usage Examples:

import kotlin.io.path.*

// Create temporary files
val tempFile = createTempFile("myapp", ".tmp")
println("Created temp file: $tempFile")

val tempConfig = createTempFile(prefix = "config", suffix = ".properties")
println("Created temp config: $tempConfig")

// Create temporary directories
val tempDir = createTempDirectory("workspace")
println("Created temp directory: $tempDir")

// Create in specific directory
val workDir = Path("/var/tmp")
val projectTemp = createTempDirectory(workDir, "project")
println("Created project temp: $projectTemp")

Install with Tessl CLI

npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-stdlib-jdk7

docs

file-io.md

file-system.md

index.md

path-operations.md

recursive-operations.md

tree-traversal.md

tile.json