# Writer

abstract class Writer

Write data to a file.

classDiagram Writer <|-- YAMLWriter YAMLWriter <|-- JSONWriter link Writer "../Writer/" link YAMLWriter "../YAMLWriter/" link JSONWriter "../JSONWriter/"

Typical use is to use the Writer factory function to instantiate an object of an appropriate derived class based on the file extension of the given path:

let writer <- Writer(path);


The whole contents of the file can be written at once with:

writer.dump(buffer);


Alternatively, where the root element of the file is an array, the contents may be written sequentially, one element at a time:

writer.push(buffer1);
writer.push(buffer2);
writer.push(buffer3);


Finally, close the file:

writer.close();


A file may not be valid for reading until the writer is closed, depending on the file format.

### Factory Functions

Name Description
Writer Create a writer for a file.

### Member Functions

Name Description
open Open a file.
dump Write the whole contents of a buffer into the file.
push Write a buffer to the file.
flush Flush accumulated writes to the file.
close Close the file.

### Factory Function Details

function Writer(path:String) -> Writer

Create a writer for a file.

• path: Path of the file.

Returns: the writer.

The file extension of path is used to determine the precise type of the returned object. Supported file extension are .json and .yml.

### Member Function Details

#### close

abstract function close()

Close the file.

#### dump

abstract function dump(buffer:Buffer)

Write the whole contents of a buffer into the file.

• buffer: Buffer to write.

#### flush

abstract function flush()

Flush accumulated writes to the file.

#### open

abstract function open(path:String)

Open a file.

• path : Path of the file.

#### push

abstract function push(buffer:Buffer)

Write a buffer to the file. This allows for sequential writing of the file, where the root element is an array. It avoids holding the entire file in memory at once.

• buffer: Buffer to write.