scalax.file

FileOps

class FileOps extends Seekable

An object for reading and writing files. FileOps provides access to Channels and streams as well as providing methods for performing simple tasks on files easily.

Obtaining a FileOps from a object does not open a file execution of methods will open a file. Thus it is important to handle NotFileException and FileNotFoundException. Depending on the method one or both exceptions must be handled.

Examples of exception handling:


 try {
  file.lines flatMap _.split(":")
 } catch {
  case FileNotFoundException => fail
  case NotFileException => fail
 }

or using the Exceptions object

import scala.util.control.Exceptions
val catcher = catching(classOf[NotFileException], classOf[FileNotFoundException])

catcher {
  file.lines flatMap _.split(":")
}

The API into 3 main sections

open() attempts to perform all actions using the open channel in order to improve the performance of the operations.

lock() performs all the actions using the same channel

Attributes
abstract
Since

1.0

Linear Supertypes
Seekable, Output, Input, AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Hide All
  2. Show all
  1. FileOps
  2. Seekable
  3. Output
  4. Input
  5. AnyRef
  6. Any
Visibility
  1. Public
  2. All

Instance Constructors

  1. new FileOps ()

Abstract Value Members

  1. def channel (openOptions: OpenOption*): SeekableByteChannelResource[SeekableByteChannel]

    Obtains a ByteChannel for read/write access to the file.

    Obtains a ByteChannel for read/write access to the file. If no OpenOptions are specified the underlying file will be opened with read/write/create/truncate options

    All OpenOption can be used

    openOptions

    the options that define how the file is opened when using the stream Default is options only

    Attributes
    abstract
  2. def fileChannel (openOptions: OpenOption*): Option[SeekableByteChannelResource[SeekableByteChannel]]

    Obtains a FileChannel for read/write access to the file.

    Obtains a FileChannel for read/write access to the file. Not all filesystems can support FileChannels therefore None will be returned if the filesystem does not support FileChannels. If no OpenOptions are specified the underlying file will be opened with read/write/create/truncate options

    All OpenOption can be used

    openOptions

    the options that define how the file is opened when using the stream Default is read/write/create/truncate

    Attributes
    abstract
  3. def inputStream (): InputStreamResource[InputStream]

    Obtains an input stream resource for reading from the file

    Obtains an input stream resource for reading from the file

    Attributes
    abstract
  4. def open [R] (openOptions: Seq[OpenOption] = WriteTruncate)(action: (OpenSeekable) ⇒ R): R

    Runs several operations as efficiently as possible.

    Runs several operations as efficiently as possible. If the filesystem permits random access then the same channel will be used to perform all operations.

    Note: only the direct file operations (bytes,lines,write,patch etc...) can be used and expected to use the same resource. The resource methods all created new streams.

    Note: not all file systems support this, if not then at worst the performance is the same as if they where performed outside an open block

    openOptions

    The options that define how the file is opened for the duration of the operation Default is Write/Create/Truncate

    action

    The function that will be executed within the block

    Attributes
    abstract
  5. def outputStream (openOptions: OpenOption*): OutputStreamResource[OutputStream]

    Obtains an OutputStreamResource for writing to the file

    Obtains an OutputStreamResource for writing to the file

    All OpenOption can be used except Read which will be ignored if present

    openOptions

    the options that define how the file is opened when using the stream The Write option is implicitly added to the set of options Default is write/create/truncate

    Attributes
    abstract
  6. def size : Option[Long]

    The number of bytes that can be read from the underlying resource.

    The number of bytes that can be read from the underlying resource.

    if length == None then it is not possible to determine the number of bytes in advance.

    Attributes
    abstract
    Definition Classes
    Input
  7. def withLock [R] (start: Long = 0, size: Long = 1, shared: Boolean = false)(block: (Seekable) ⇒ R): Option[R]

    Performs an operation on the file with a FileLock

    Performs an operation on the file with a FileLock

    Not all filesystems support locking. If not then None will be returned by the method

    The defaults will lock the entire file with an exclusive lock. It is possible to modify the lock so that it only locks part of the file and may be a shared lock. Not all filesystems support shared locks but if that is the case the lock will automatically be upgraded to a exclusiveLock

    The sematics of this locking behavious are very similar to those in the java.nio.channels.FileLock It is recommended that those javadocs are read and the warnings present in those docs are followed.

    start

    the start position of the lock. Must be a non-negative Long

    size

    the length in bits the lock. If -1 then the entire file from start to the end will be locked

    shared

    If true then a shared lock will be obtained if possible. If shared locks are not supported then an exclusive lock will be obtained

    returns

    the result the result from the block or None if the filesystem does not support locking

    Attributes
    abstract

Concrete Value Members

  1. def != (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  2. def != (arg0: Any): Boolean

    Attributes
    final
    Definition Classes
    Any
  3. def ## (): Int

    Attributes
    final
    Definition Classes
    AnyRef → Any
  4. def == (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  5. def == (arg0: Any): Boolean

    Attributes
    final
    Definition Classes
    Any
  6. def append (string: String)(implicit codec: Codec = Codec.default): Unit

    Append a string to the end of the Seekable object.

    Append a string to the end of the Seekable object.

    string

    the data to write

    codec

    the codec of the string to be written. The string will be converted to the encoding of codec

    Definition Classes
    Seekable
  7. def append [T] (data: T)(implicit converter: OutputConverter[T]): Unit

    Append bytes to the end of a file

    Append bytes to the end of a file

    Important: The use of an Array is highly recommended because normally arrays can be more efficiently written using the underlying APIs

    data

    The data to write. This can be any type that has a OutputConverter associated with it. There are predefined $outputConverters for several types. See the OutputConverter object for the predefined types and for objects to simplify implementing custom OutputConverter

    $converterParam

    Definition Classes
    Seekable
  8. def appendChannel [U] (f: (SeekableByteChannel) ⇒ U): U

    Attributes
    protected
    Definition Classes
    Seekable
  9. def appendIntsAsBytes (data: Int*): Unit

    Since the OutputConverter object defined for writing Ints encodes Ints using 4 bytes this method is provided to simply write an array of Ints as if they are Bytes.

    Since the OutputConverter object defined for writing Ints encodes Ints using 4 bytes this method is provided to simply write an array of Ints as if they are Bytes. In other words just taking the first byte. This is pretty common in Java.io style IO. IE

     outputStream.write(1) 
    

    1 is written as a single byte.

    Definition Classes
    Seekable
  10. def appendStrings (strings: Traversable[String], separator: String = "")(implicit codec: Codec = Codec.default): Unit

    Append several strings to the end of the Seekable object.

    Append several strings to the end of the Seekable object.

    strings

    The strings to write

    separator

    A string to add between each string. It is not added to the before the first string or after the last.

    codec

    The codec of the strings to be written. The strings will be converted to the encoding of codec

    Definition Classes
    Seekable
  11. def asInstanceOf [T0] : T0

    Attributes
    final
    Definition Classes
    Any
  12. def byteArray : Array[Byte]

    This method aspires to be the fastest way to read a stream of known length into memory.

    This method aspires to be the fastest way to read a stream of known length into memory.

    Definition Classes
    Input
  13. def bytes : ResourceView[Byte]

    Obtains a Traversable for conveniently processing the resource as bytes.

    Obtains a Traversable for conveniently processing the resource as bytes.

    returns

    an non-strict traversable over all the bytes

    Definition Classes
    FileOpsSeekableInput
  14. def bytesAsInts : ResourceView[Int]

    Obtains a Traversable for conveniently processing the file as Ints.

    Obtains a Traversable for conveniently processing the file as Ints.

    returns

    an non-strict traversable over all the bytes with the bytes being represented as Ints

    Definition Classes
    FileOpsSeekableInput
  15. def chars (implicit codec: Codec): ResourceView[Char]

    The characters in the object.

    The characters in the object.

    If the codec is not the same as the source codec (the codec of the underlying data) then the characters will converted to the desired codec.

    codec

    The codec representing the desired encoding of the characters @return an traversable of all the characters

    Definition Classes
    FileOpsSeekableInput
  16. def clone (): AnyRef

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  17. def copyDataFrom (input: Input, finalize: Boolean = false): Unit

    Copy data from an input to this output.

    Copy data from an input to this output. Input will call this method from copyTo if it does not know how to efficiently copy the data. This method thus will copy as effiently as possible and if all else fails simply write the bytes

    input

    the source to read data from

    finalize

    do not forward request to input's copyTo method. Often only one end of the transaction will know how to efficiently transfer data so a common pattern is to check the input and see if the type of the Input object is a known type. If not then the input object will be sent the request. However, to prevent an infinite loop the finalize will be set to true so the request is not then forwarded back to copyFrom

    Definition Classes
    Output
  18. def copyDataTo (output: Output, finalize: Boolean = false): Unit

    Copy all data from this Input object to the Output object as efficiently as possible.

    Copy all data from this Input object to the Output object as efficiently as possible.

    output

    output sink to copy the data to

    finalize

    do not forward request to output's copyFrom method. Often only one end of the transaction will know how to efficiently transfer data so a common pattern is to check the output and see if the type of the Output object is a known type. If not then the output object will be sent the request. However, to prevent an infinite loop the finalize will be set to true so the request is not then forwarded back to copyTo

    Definition Classes
    Input
  19. def eq (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  20. def equals (arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  21. def finalize (): Unit

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  22. def getClass (): java.lang.Class[_]

    Attributes
    final
    Definition Classes
    AnyRef → Any
  23. def hashCode (): Int

    Definition Classes
    AnyRef → Any
  24. def insert [T] (position: Long, data: T)(implicit converter: OutputConverter[T]): Any

    Inserts data at a position in the Seekable.

    Inserts data at a position in the Seekable. The actual position in the Seekable where the data is inserted depends on the type of data being written. For example if Longs are being written then position calculated as position * 8

    Important: The use of an Array is highly recommended because normally arrays can be more efficiently written using the underlying APIs

    position

    The position where the data is inserted into the Seekable. The actual position in the Seekable where the data is inserted depends on the type of data being written. For example if Longs are being written then position calculated as position * 8

    data

    The data to write. This can be any type that has a OutputConverter associated with it. There are predefined $outputConverters for several types. See the OutputConverter object for the predefined types and for objects to simplify implementing custom OutputConverter

    $converterParam

    Definition Classes
    Seekable
  25. def insert (position: Long, string: String)(implicit codec: Codec = Codec.default): Unit

    Inserts a string at a position in the Seekable.

    Inserts a string at a position in the Seekable. This is a potentially inefficient because of the need to count characters. If the codec is not a fixed sized codec (for example UTF8) each character must be converted in the file up to the point of insertion.

    position

    The position in the file to perform the insert. A position of 2 will insert the character after the second character (not byte).

    string

    The string that will be inserted into the Seekable

    codec

    The codec to use for determining the location for inserting the string and for encoding the string as bytes

    Definition Classes
    Seekable
  26. def insertIntsAsBytes (position: Long, data: Int*): Any

    Since the OutputConverter object defined for writing Ints encodes Ints using 4 bytes this method is provided to simply write an array of Ints as if they are Bytes.

    Since the OutputConverter object defined for writing Ints encodes Ints using 4 bytes this method is provided to simply write an array of Ints as if they are Bytes. In other words just taking the first byte. This is pretty common in Java.io style IO. IE

     outputStream.write(1) 
    

    1 is written as a single byte.

    Definition Classes
    Seekable
  27. def isInstanceOf [T0] : Boolean

    Attributes
    final
    Definition Classes
    Any
  28. def lines (terminator: Terminator = Terminators.Auto, includeTerminator: Boolean = false)(implicit codec: Codec = Codec.default): ResourceView[String]

    Obtain an non-strict traversable for iterating through the lines in the object

    Obtain an non-strict traversable for iterating through the lines in the object

    If the codec is not the same as the source codec (the codec of the underlying data) then the characters will converted to the desired codec.

    terminator

    The strategy for determining the end of line Default is to auto-detect the EOL

    includeTerminator

    if true then the line will end with the line terminator Default is false

    @return a non-strict traversable for iterating through all the lines

    codec

    The codec representing the desired encoding of the characters

    Definition Classes
    Input
  29. def ne (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  30. def notify (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
  31. def notifyAll (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
  32. def open [U] (f: (OpenSeekable) ⇒ U): U

    Execute the function 'f' passing an Seekable instance that performs all operations on a single opened connection to the underlying resource.

    Execute the function 'f' passing an Seekable instance that performs all operations on a single opened connection to the underlying resource. Typically each call to one of the Seekable's methods results in a new connection. For example if write it called typically it will write to the start of the seekable but in open it will write to the current position.

    Even if the underlying resource is an appending, using open will be more efficient since the connection only needs to be made a single time.

    f

    the function to execute on the new Output instance (which uses a single connection)

    returns

    the result of the function

    Definition Classes
    Seekable
  33. def openOutput [U] (f: (Output) ⇒ U): U

    Execute the function 'f' passing an Output instance that performs all operations on a single opened connection to the underlying resource.

    Execute the function 'f' passing an Output instance that performs all operations on a single opened connection to the underlying resource. Typically each call to one of the Output's methods results in a new connection. For example if the underlying OutputStream truncates the file each time the connection is made then calling write two times will result in the contents of the second write overwriting the second write.

    Even if the underlying resource is an appending, using open will be more efficient since the connection only needs to be made a single time.

    f

    the function to execute on the new Output instance (which uses a single connection)

    returns

    the result of the function

    Definition Classes
    Output
  34. def patch [T] (position: Long, data: T, overwrite: Overwrite)(implicit converter: OutputConverter[T]): Unit

    Update a portion of the file content at the declared location.

    Update a portion of the file content at the declared location. This is the most flexible of the random access methods but is also (probably) the trickiest to fully understand. That said it behaves (almost) identical to a scala.collection.Seq.patch method, so if you understand that you should not have difficulty understanding this method.

    Important: The use of an Array is highly recommended because normally arrays can be more efficiently written using the underlying APIs

    To append data the position must >= size

    If the position is within the file but the position + bytes.length is beyond the end of the file the file will be enlarged so that the entire string can fit in the file

    The write begins at the position indicated. So if position = 0 then the write will begin at the first byte of the file.

    position

    The start position of the update starting at 0. The position must be within the file or == size (for appending)

    data

    The data to write. This can be any type that has a OutputConverter associated with it. There are predefined $outputConverters for several types. See the OutputConverter object for the predefined types and for objects to simplify implementing custom OutputConverter

    overwrite

    The strategy that dictates how many characters/bytes/units are overwritten $converterParam

    Definition Classes
    Seekable
  35. def patch (position: Long, string: String, overwrite: Overwrite)(implicit codec: Codec = Codec.default): Unit

    Update a portion of the file content at the declared location.

    Update a portion of the file content at the declared location. This is the most flexible of the random access methods but is also (probably) the trickiest to fully understand. That said it behaves (almost) identical to a scala.collection.Seq.patch method, so if you understand that you should not have difficulty understanding this method.

    If the position is beyond the end of the file a BufferUnderflow Exception will be thrown

    If the position is within the file but the position + string.getBytes(codec).length is beyond the end of the file the file will be enlarged so that the entire string can fit in the file

    The write begins at the position indicated. So if position = 0 then the write will begin at the first byte of the file.

    position

    The start position of the update starting at 0. The position is the position'th character in the file using the codec for decoding the file The position must be within the file.

    string

    The string to write to the file starting at position.

    overwrite

    The strategy that dictates how many characters/bytes/units are overwritten

    codec

    The codec to use for decoding the underlying data into characters

    Definition Classes
    Seekable
  36. def patchIntsAsBytes (position: Long, overwrite: Overwrite, data: Int*): Unit

    Since the OutputConverter object defined for writing Ints encodes Ints using 4 bytes this method is provided to simply write an array of Ints as if they are Bytes.

    Since the OutputConverter object defined for writing Ints encodes Ints using 4 bytes this method is provided to simply write an array of Ints as if they are Bytes. In other words just taking the first byte. This is pretty common in Java.io style IO. IE

     outputStream.write(1) 
    

    1 is written as a single byte.

    Definition Classes
    Seekable
  37. def readWriteChannel [U] (f: (SeekableByteChannel) ⇒ U): U

    Attributes
    protected
    Definition Classes
    Seekable
  38. def slurpString (implicit codec: Codec = Codec.default): String

    Loads all the characters into memory.

    Loads all the characters into memory. There is no protection against loading very large files/amounts of data.

    codec

    The codec representing the desired encoding of the characters

    Definition Classes
    Input
  39. def synchronized [T0] (arg0: ⇒ T0): T0

    Attributes
    final
    Definition Classes
    AnyRef
  40. def tempFile (): Input with Output

    Create a temporary file to use for performing certain operations.

    Create a temporary file to use for performing certain operations. It should be as efficient as possible to copy from the temporary file to this Seekable and vice-versa. Can be overridden for performance

    Attributes
    protected
    Definition Classes
    Seekable
  41. def toString (): String

    Definition Classes
    AnyRef → Any
  42. def truncate (position: Long): Unit

    Truncate/Chop the Seekable to the number of bytes declared by the position param

    Truncate/Chop the Seekable to the number of bytes declared by the position param

    Definition Classes
    Seekable
  43. def truncateString (position: Long)(implicit codec: Codec = Codec.default): Unit

    Definition Classes
    Seekable
  44. def underlyingChannel (append: Boolean): OpenedResource[SeekableByteChannel]

    Attributes
    protected
    Definition Classes
    FileOpsSeekable
  45. def underlyingOutput : OutputStreamResource[OutputStream]

    Attributes
    protected
    Definition Classes
    FileOpsSeekableOutput
  46. def wait (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
    Annotations
    @throws()
  47. def wait (arg0: Long, arg1: Int): Unit

    Attributes
    final
    Definition Classes
    AnyRef
    Annotations
    @throws()
  48. def wait (arg0: Long): Unit

    Attributes
    final
    Definition Classes
    AnyRef
    Annotations
    @throws()
  49. def write (string: String)(implicit codec: Codec = Codec.default): Unit

    Writes a string.

    Writes a string.

    string

    the data to write

    codec

    the codec of the string to be written. The string will be converted to the encoding of sourceCodec Default is sourceCodec

    Definition Classes
    Output
  50. def write [T] (data: T)(implicit writer: OutputConverter[T]): Unit

    Write data to the underlying object.

    Write data to the underlying object. Each time write is called the resource is reopened, in the case of a file this means that the file will be opened and truncated. The

    In the case of writing ints and bytes it is often recommended to write arrays of data since normally the underlying object can write arrays of bytes or integers most efficiently.

    Since Characters require a codec to write to an OutputStream characters cannot be written with this method unless a OutputWriterFunction.CharFunction object is provided as the writer.

    data

    The data to write to underlying object. Any data that has a resolvable OutputConverter can be written. See the OutputConverter object for the defined OutputConverter implementations and classes to assist implementing more.

    writer

    The strategy used to write the data to the underlying object. Many standard data-types are implicitly resolved and do not need to be supplied

    Definition Classes
    Output
    See also

    #writeChars for more on writing characters

  51. def writeChars (characters: TraversableOnce[Char])(implicit codec: Codec = Codec.default): Unit

    Definition Classes
    Output
  52. def writeIntsAsBytes (data: Int*): Unit

    Since the OutputConverter object defined for writing Ints encodes Ints using 4 bytes this method is provided to simply write an array of Ints as if they are Bytes.

    Since the OutputConverter object defined for writing Ints encodes Ints using 4 bytes this method is provided to simply write an array of Ints as if they are Bytes. In other words just taking the first byte. This is pretty common in Java.io style IO. IE

     outputStream.write(1) 
    

    1 is written as a single byte.

    Definition Classes
    Output
  53. def writeStrings (strings: Traversable[String], separator: String = "")(implicit codec: Codec = Codec.default): Unit

    Write several strings.

    Write several strings.

    strings

    The data to write

    separator

    A string to add between each string. It is not added to the before the first string or after the last.

    codec

    The codec of the strings to be written. The strings will be converted to the encoding of sourceCodec

    Definition Classes
    Output

Inherited from Seekable

Inherited from Output

Inherited from Input

Inherited from AnyRef

Inherited from Any