FileMethods

final implicit class FileMethods(file: File) extends AnyVal

Provides extension methods for java.io.File.

See also:

PathMethods

class AnyVal
trait Matchable
class Any

Value members

Concrete methods

def /(child: String): File

Creates new file appending child path.

Creates new file appending child path.

Returns:

file

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

Appends supplied bytes to file.

Appends supplied bytes to file.

Returns:

file

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

Appends supplied characters to file.

Appends supplied characters to file.

Returns:

file

def <<(chars: CharSequence): File

Appends supplied characters to file.

Appends supplied characters to file.

Returns:

file

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

Appends contents of supplied InputStream to file.

Appends contents of supplied InputStream to file.

Returns:

file

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

Appends contents of supplied Reader to file.

Appends contents of supplied Reader to file.

Returns:

file

def <<(source: File): File

Appends supplied source to file.

Appends supplied source to file.

Returns:

file

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: File => 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 file is not directory or if directory listing is not available

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, File) => 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 file is not directory or if directory listing is not available

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: File => Unit): Unit

Invokes supplied function for each file in directory.

Invokes supplied function for each file in directory.

Value parameters:
f

function

Throws:
java.io.IOException

if file is not directory or if directory listing is not available

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

Reads file and invokes supplied function for each line.

Reads file and invokes supplied function for each line.

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

Value parameters:
f

function

def getBytes(): Array[Byte]

Reads file and returns its bytes.

Reads file and returns its bytes.

def getLines(): Seq[String]

Gets lines in file.

Gets lines in file.

def getText(): String

Reads file and returns its text.

Reads file and returns its text.

def mapFiles[T](f: File => 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 file is not directory or if directory listing is not available

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 withDataInput[T](f: DataInput => T): T

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

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

Value parameters:
f

function

Returns:

value from supplied function

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

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

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

Value parameters:
f

function

Returns:

value from supplied function

def withDataOutput[T](append: Boolean)(f: DataOutput => T): T

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

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

Value parameters:
append

if true, output is appended to end of file; otherwise, if false, file is truncated and output is written at beginning of file

f

function

Returns:

value from supplied function

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

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

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

Value parameters:
f

function

Returns:

value from supplied function

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

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

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

Value parameters:
f

function

Returns:

value from supplied function

def withOutputStream[T](append: Boolean)(f: OutputStream => T): T

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

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

Value parameters:
append

if true, output is appended to end of file; otherwise, if false, file is truncated and output is written at beginning of file

f

function

Returns:

value from supplied function

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

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

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

Value parameters:
f

function

Returns:

value from supplied function

def withPrintWriter[T](append: Boolean)(f: PrintWriter => T): T

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

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

Value parameters:
append

if true, output is appended to end of file; otherwise, if false, file is truncated and output is written at beginning of file

f

function

Returns:

value from supplied function

def withRandomAccess[T](mode: String)(f: RandomAccessFile => T): T

Opens RandomAccessFile with specified access mode and passes it to function. File is closed on function's return.

Opens RandomAccessFile with specified access mode and passes it to function. File is closed on function's return.

Value parameters:
f

function

mode

access mode

Returns:

value from supplied function

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

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

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

Value parameters:
f

function

Returns:

value from supplied function

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

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

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

Value parameters:
f

function

Returns:

value from supplied function

def withWriter[T](append: Boolean)(f: BufferedWriter => T): T

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

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

Value parameters:
append

if true, output is appended to end of file; otherwise, if false, file is truncated and output is written at beginning of file

f

function

Returns:

value from supplied function