file_stream


file_stream(path, @key options, buffer_mode, transcoder, permissions = 0o666)
      

Return a new input/output stream for the file at path.

If present, options must be a list of symbols that control aspects of file operations. The options that are valid are 'create, 'no_create, 'maybe_create, 'append and 'truncate.

The 'create option requires that the file does not exist (otherwise an exception is raised) and creates the file when it is opened.

The 'no_create option requires that the file exist (otherwise an exception is raised).

The 'maybe_create option will create the file if it does not exist. This is the default option.

The 'append option controls whether output will be added to the end of the file. This is useful for writing to log files that might be open by more than one process. By default appending is turned-off.

The 'truncate option controls whether the file will be truncated when it is opened. This option is turned-on by default when no 'append option is provided and turned-off when 'append is not provided.

Buffer_mode is a symbol that controls buffering. Valid values are 'block, 'line and 'none. By default, buffering is turned off.

If transcoder is present and is not false, it must be a transcoder object and file_writer will return a textual output stream that will be encoded with transcoder. Otherwise, a binary output stream is returned.

Permissions control the UNIX permissions that will be attached to the file if it is created. The default value of this setting is 0o666.

Examples:


let s = file_stream("out.txt", options = ['create])
showln(stream = s, "hello, world")
set_stream_position(s, 0)
read_line(s)
// hello, world
      

Also see:

file_reader
file_writer


Core Module Index | Contents