Represents a 'Directory'
Represents a 'Directory'
the method signature from foreach
, map
and flatMap
shown here are simplified
this functions doesn't work recursive by default. for a recursive behaviour, use their counterpart functions
with the 'R' suffix: lsR
, foreachR
, mapR
or collectR
.
to control the recursive level, you can give the lsR
, foreachR
, mapR
or collectR
function a 'depth' argument.
you can give foreach
, map
and flatMap
a traverse function which receives a FSEntryImpl
or a Try[FSEntryImpl]
.
assume the following directory tree:
root@main:/tmp/sclib-example # ls -lR /tmp/sclib-example/ total 1 drwx------ 2 j wheel 5 May 4 10:51 pub d--------- 2 j wheel 5 May 4 10:51 sec /tmp/sclib-example/pub: total 2 -rw-r--r-- 1 j wheel 0 May 4 10:51 a -rw-r--r-- 1 j wheel 0 May 4 10:51 b -rw-r--r-- 1 j wheel 0 May 4 10:51 c /tmp/sclib-example/sec: total 1 ---------- 1 j wheel 0 May 4 10:51 a
fail on first error:
if you get it a function which receives a FSEntryImpl
and an exception occurs, the function execution
stops and a Failure
are returned.
scala> import sclib.io.fs._ scala> dir("/tmp/sclib-example").get.foreachR(println(_: FSEntryImpl)) FSDir(/tmp/sclib-example/sec) res0: scala.util.Try[Unit] = Failure(java.nio.file.AccessDeniedException: /tmp/sclib-example/sec)
exceptions intercepted:
if you get it a function which receives a Try[FSEntryImpl]
and an exception occurs, the function execution
continues.
scala> import sclib.io.fs._ scala> import scala.util.Try scala> dir("/tmp/sclib-example").get.foreachR(println(_: Try[FSEntryImpl])) Success(FSDir(/tmp/sclib-example/sec)) Failure(java.nio.file.AccessDeniedException: /tmp/sclib-example/sec) Success(FSDir(/tmp/sclib-example/pub)) Success(FSFile(/tmp/sclib-example/pub/c)) Success(FSFile(/tmp/sclib-example/pub/b)) Success(FSFile(/tmp/sclib-example/pub/a))
check the member documentation for examples
File System Entry
Any FSEntry Implementation
Represents a 'File'
Represents a 'File'
the functions write
, writeLines
, append
and appendLines
expects a type-class instance (from Writable)
for their to be written payload in scope. instances for string
, char
, short
, int
, long
, float
,
double
and some collection types are already defined (see Writable$).
the write
and append
functions don't add a newline at the end / between the sequences.
for functions which add newlines use writeLines
and appendLines
.
scala> import sclib.io._ scala> for { | fh <- file("/tmp/example") | _ <- fh.writeLines("1. apple") // string | _ <- fh.appendLines(List("2. banana", "3. cherry")) // list of string | _ <- fh.append(4) // int | _ <- fh.append('.') // char | _ <- fh.append(Vector(' ', 'd', 'o', 'g')) // vector of char | content <- fh.slurp | _ <- fh.delete | res0: scala.util.Try[String] = Success(1. apple 2. banana 3. cherry 4. dog)
check the member documentation for examples
a save file-system iterator.
a save file-system iterator.
it wraps every file operation in a Try
.
by default, the iterator start in the given directory, and walk recursively (deep first) over all entries. you can control the recursive level with the 'depth' argument, where 1 means only the content from the given directory. to start with the given directory, use 'includeStartDir = true'.
assume the following directory tree:
/tmp/sclib-example/a1/b1/a1b1file /tmp/sclib-example/a1/b1/c1/a1b1c1file /tmp/sclib-example/a2/b2/a2b2file
scala> FSIterator(dir("/tmp/sclib-example").get).foreach(println) Success(FSDir(/tmp/sclib-example/a1)) Success(FSDir(/tmp/sclib-example/a1/b1)) Success(FSFile(/tmp/sclib-example/a1/b1/a1b1file)) Success(FSDir(/tmp/sclib-example/a1/b1/c1)) Success(FSFile(/tmp/sclib-example/a1/b1/c1/a1b1c1file)) Success(FSDir(/tmp/sclib-example/a2)) Success(FSDir(/tmp/sclib-example/a2/b2)) Success(FSFile(/tmp/sclib-example/a2/b2/a2b2file))
scala> FSIterator(dir("/tmp/sclib-example").get, depth = 2, includeStartDir = true).foreach(println) Success(FSDir(/tmp/sclib-example)) Success(FSDir(/tmp/sclib-example/a1)) Success(FSDir(/tmp/sclib-example/a1/b1)) Success(FSDir(/tmp/sclib-example/a2)) Success(FSDir(/tmp/sclib-example/a2/b2))
directory to start
the maximum number of directory levels to visit
type-class for the FSFile.write / FSFile.append functions
utility to calculate PosixFilePermission
s from unix-style notation:
utility to calculate PosixFilePermission
s from unix-style notation:
- FSPerm.calc(644) -> Seq(OWNER_READ, OWNER_WRITE, GROUP_READ, OTHERS_READ) - FSPerm.mod("a=r,u+w") -> Seq(OWNER_READ, OWNER_WRITE, GROUP_READ, OTHERS_READ)
Instances for the Writable type-class
initialize a directory-handle from a given path
initialize a directory-handle from a given path
if the given path exists, and is a file, a failure are returned.
this doesn't create the directory - use FSEntry.create(n:* / FSDir.createTemp to create it
initialize a directory-handle from a given path
initialize a directory-handle from a given path
if the given path exists, and is a file, a failure are returned.
this doesn't create the directory - use FSEntry.create(n:* / FSDir.createTemp to create it
initialize a directory-handle from a given path
initialize a directory-handle from a given path
if the given path exists, and is a file, a failure are returned.
this doesn't create the directory - use FSEntry.create(n:* / FSDir.createTemp to create it
initialize a file-handle from a given path
initialize a file-handle from a given path
if the given path exists, and is a directory, a failure are returned.
this doesn't create the file - use FSEntry.create(n:* / FSFile.createTemp / any write method to create it
initialize a file-handle from a given path
initialize a file-handle from a given path
if the given path exists, and is a directory, a failure are returned.
this doesn't create the file - use FSEntry.create(n:* / FSFile.createTemp / any write method to create it
initialize a file-handle from a given path
initialize a file-handle from a given path
if the given path exists, and is a directory, a failure are returned.
this doesn't create the file - use FSEntry.create(n:* / FSFile.createTemp / any write method to create it
working with files and directories
get a file / directory handle
to get a file handle, use any of the following functions:
for a directory handle:
example:
this functions returns a
Failure
if the requested entry exists, but has a wrong type.work with a handle
all functions which can throw a exception are wrapped in a
Try
, so it's easy to compose.sclib.io.fs.FSDir
sclib.io.fs.FSFile