WritableFile

public final class WritableFile: File, WritableStream, MutableFileSystemItem

A class for writing text to a file.

  • The path to the file

    Declaration

    Swift

    public internal(set) var path: FilePath
  • The encoding for the text in the file.

    Declaration

    Swift

    public var encoding: String.Encoding = .utf8
  • The type of file or file-like item this is.

    Declaration

    Swift

    public let type: FileType
  • Opens the file at ‘path’ for writing.

    Parameter

    Parameter path: the path to the file.

    Throws

    FileSystemError.notFound, .isDirectory, .invalidAccess.

    Declaration

    Swift

    public convenience init(open path: FilePath) throws

    Parameters

    path

    the path to the file.

  • Creates a new WritableFile which writes to the provided file handle. The path is /dev/fd/ + the file handle’s filedescriptor.

    Declaration

    Swift

    public convenience init(_ filehandle: FileHandle)
  • Creates a new file at ‘path’ for writing.

  • Parameters:

    • path: The path where the new file should be created.
    • ifExists: What to do if it already exists: open, throw error or replace.
  • Throws

    FileSystemError.isDirectory, .couldNotCreate, .alreadyExists, .outsideSandbox.

    Declaration

    Swift

    public convenience init(create path: FilePath, ifExists: AlreadyExistsOptions) throws

    Parameters

    path

    The path where the new file should be created.

    ifExists

    What to do if it already exists: open, throw error or replace.

  • Creates a new file at ‘stringpath’ for writing.

  • Parameters:

    • stringpath: The path where the new file should be created.
    • ifExists: What to do if it already exists: open, throw error or replace.
  • Throws

    FileSystemError.isDirectory, .couldNotCreate, .alreadyExists, .outsideSandbox.

    Declaration

    Swift

    public convenience init(create stringpath: String, ifExists: AlreadyExistsOptions) throws

    Parameters

    stringpath

    The path where the new file should be created.

    ifExists

    What to do if it already exists: open, throw error or replace.

  • Deletes this file. For ever.

    Declaration

    Swift

    public func delete() throws
  • Appends the string to the file. Nothing is overwritten, just added to the end of the file.

    Declaration

    Swift

    public func write(_ string: String)
  • Closes the file. No more writing.

    Declaration

    Swift

    public func close()
  • Replaces the entire contents of the file with the string. - warning: The current contents of the file will be lost. - warning: Will crash if this is not a regular file.

    Declaration

    Swift

    public func overwrite(_ string: String)
  • A WritableStream which writes to standard output.

    Declaration

    Swift

    static public var stdout: WritableStream = StdoutStream.default
  • A WritableStream which writes to standard error.

    Declaration

    Swift

    static public var stderror: WritableStream =