PathMethods

final implicit class PathMethods(path: Path) extends AnyVal

Provides extension methods for java.nio.file.Path.

See also:

FileMethods

class AnyVal
trait Matchable
class Any

Value members

Concrete methods

def /(child: String): Path

Creates new file appending child path.

Creates new file appending child path.

Returns:

file

def <<(bytes: Array[Byte]): Path

Appends supplied bytes to file.

Appends supplied bytes to file.

Returns:

path

def <<(chars: Array[Char]): Path

Appends supplied characters to file.

Appends supplied characters to file.

Returns:

path

def <<(chars: CharSequence): Path

Appends supplied characters to file.

Appends supplied characters to file.

Returns:

path

def <<(in: InputStream)(using bufferSize: BufferSize): Path

Appends contents of supplied InputStream to file.

Appends contents of supplied InputStream to file.

Returns:

path

def <<(in: Reader)(using bufferSize: BufferSize): Path

Appends contents of supplied Reader to file.

Appends contents of supplied Reader to file.

Returns:

path

def <<(source: Path): Path

Appends supplied source to file.

Appends supplied source to file.

Returns:

path

Throws:
IOException

if source is same as target

def filterLines(p: String => Boolean): Seq[String]

Filters lines in file using supplied predicate.

Filters lines in file using supplied predicate.

Value parameters:
p

predicate

def flatMapFiles[T](f: Path => Iterable[T]): Seq[T]

Builds collection using elements mapped from files in directory.

Builds collection using elements mapped from files in directory.

Value parameters:
f

function

Throws:
java.io.IOException

if path is not to a directory

def flatMapLines[T](f: String => Iterable[T]): Seq[T]

Builds collection using elements mapped from lines in file.

Builds collection using elements mapped from lines in file.

Value parameters:
f

function

def foldFiles[T](init: T)(op: (T, Path) => T): T

Folds files in directory to single value using given initial value and binary operator.

Folds files in directory to single value using given initial value and binary operator.

Value parameters:
init

initial value

op

binary operator

Returns:

init if no files; otherwise, last value returned from op

Throws:
java.io.IOException

if path is not to a directory

def foldLines[T](init: T)(op: (T, String) => T): T

Folds lines in file to single value using given initial value and binary operator.

Folds lines in file to single value using given initial value and binary operator.

Value parameters:
init

initial value

op

binary operator

Returns:

init if file is empty; otherwise, last value returned from op

def forEachFile(f: Path => Unit): Unit

Opens directory stream to path and invokes supplied function for each file in directory.

Opens directory stream to path and invokes supplied function for each file in directory.

Value parameters:
f

function

Throws:
java.io.IOException

if path is not to a directory

def forEachLine(f: String => Unit): Unit

Reads file at path and invokes supplied function for each line.

Reads file at path and invokes supplied function for each line.

The line content, excluding line separator, is passed to function.

Value parameters:
f

function

def forFiles(glob: String)(f: Path => Unit): Unit

Invokes supplied function for each file in directory satisfying glob.

Invokes supplied function for each file in directory satisfying glob.

Value parameters:
f

function

glob

glob pattern

Throws:
java.io.IOException

if path is not to a directory

def getBytes(): Array[Byte]

Reads file at path and returns its bytes.

Reads file at path and returns its bytes.

def getLines(): Seq[String]

Gets lines in file.

Gets lines in file.

def getText(): String

Reads file at path and returns its text.

Reads file at path and returns its text.

def mapFiles[T](f: Path => T): Seq[T]

Maps each file in directory using supplied function.

Maps each file in directory using supplied function.

Value parameters:
f

function

Throws:
java.io.IOException

if path is not to a directory

def mapLines[T](f: String => T): Seq[T]

Maps each line in file using supplied function.

Maps each line in file using supplied function.

Value parameters:
f

function

def setBytes(bytes: Array[Byte]): Unit

Sets file content to supplied bytes.

Sets file content to supplied bytes.

def setText(text: String): Unit

Sets file content to supplied text.

Sets file content to supplied text.

def withChannel[T](options: OpenOption*)(f: FileChannel => T): T

Opens FileChannel to file at path and passes it to supplied function. Channel is closed on function's return.

Opens FileChannel to file at path and passes it to supplied function. Channel is closed on function's return.

Value parameters:
f

function

options

open options

Returns:

value from supplied function

def withDataInput[T](options: OpenOption*)(f: DataInput => T): T

Opens DataInput to file at path and passes it to supplied function. Underlying input stream is closed on function's return.

Opens DataInput to file at path and passes it to supplied function. Underlying input stream is closed on function's return.

Value parameters:
f

function

options

open options

Returns:

value from supplied function

def withDataOutput[T](options: OpenOption*)(f: DataOutput => T): T

Opens DataOutput to file at path and passes it to supplied function. Underlying output stream is closed on function's return.

Opens DataOutput to file at path and passes it to supplied function. Underlying output stream is closed on function's return.

Value parameters:
f

function

options

open options

Returns:

value from supplied function

def withInputStream[T](options: OpenOption*)(f: InputStream => T): T

Opens InputStream to file at path and passes it to supplied function. Input stream is closed on function's return.

Opens InputStream to file at path and passes it to supplied function. Input stream is closed on function's return.

Value parameters:
f

function

options

open options

Returns:

value from supplied function

def withOutputStream[T](options: OpenOption*)(f: OutputStream => T): T

Opens OutputStream to file at path and passes it to supplied function. Output stream is closed on function's return.

Opens OutputStream to file at path and passes it to supplied function. Output stream is closed on function's return.

Value parameters:
f

function

options

open options

Returns:

value from supplied function

def withPrintWriter[T](options: OpenOption*)(f: PrintWriter => T): T

Opens PrintWriter to file at path and passes it to supplied function. Writer is closed on function's return.

Opens PrintWriter to file at path and passes it to supplied function. Writer is closed on function's return.

Value parameters:
f

function

options

open options

Returns:

value from supplied function

def withReader[T](options: OpenOption*)(f: BufferedReader => T): T

Opens BufferedReader to file at path and passes it to supplied function. Reader is closed on function's return.

Opens BufferedReader to file at path and passes it to supplied function. Reader is closed on function's return.

Value parameters:
f

function

options

open options

Returns:

value from supplied function

def withVisitor(visitor: PartialFunction[FileVisitEvent, FileVisitResult]): Unit

Walks file tree starting at path and invokes supplied visitor function for each event encountered.

Walks file tree starting at path and invokes supplied visitor function for each event encountered.

If supplied visitor does not handle an event, then it is treated as if it returned FileVisitResult.CONTINUE.

import java.nio.file.{ FileVisitResult, Paths }
import little.io.FileVisitEvent.{ PreVisitDirectory, VisitFile }
import little.io.PathMethods

val sourceDir = Paths.get("src")

sourceDir.withVisitor {
 case PreVisitDirectory(dir, attrs) =>
   if dir.getFileName.toString == "test" then
     FileVisitResult.SKIP_SUBTREE
   else
     println(s"Listing files in ${dir.getFileName} directory...")
     FileVisitResult.CONTINUE

 case VisitFile(file, attrs) =>
   println(s"${file.getFileName} is ${attrs.size} bytes.")
   FileVisitResult.CONTINUE
}
Value parameters:
visitor

file visitor

def withWatcher(events: Kind[_]*)(watcher: WatchEvent[_] => Unit): WatchHandle

Watchs file at path for specified events.

Watchs file at path for specified events.

Value parameters:
events

kinds of events to watch

watcher

event watcher

Returns:

watch handle

def withWriter[T](options: OpenOption*)(f: BufferedWriter => T): T

Opens BufferedWriter to file at path and passes it to supplied function. Writer is closed on function's return.

Opens BufferedWriter to file at path and passes it to supplied function. Writer is closed on function's return.

Value parameters:
f

function

options

open options

Returns:

value from supplied function