Class File

The File class provides a wrapper over a C FILE * struct. A File is closed automatically when a scope exits (though not immediately). However, it is also possible to manually close a File.

public define close

Close self if it is open, or do nothing if already closed.

For standard streams, this marks the File as closed, but does not actually close the stream. Embedders, therefore, do not need to worry about standard streams being altered or closed by Lily.

public define each_line(fn: Function(ByteString))

Read each line of text from self, passing it down to fn for processing.

public define flush

This function writes all buffered data associated with the File provided.

public static define open(path: String, mode: String): File

Attempt to open path using the mode given. mode may be one of the following:

public define print(data: A)

Attempt to write the contents of data to the file provided. data is written with a newline at the end.

public define read(size: *Integer): ByteString

Read size bytes from self. If size is negative, then the full contents of self are read. This stops if either size bytes are read, or the end of self is reached.

public define read_line: ByteString

Attempt to read a line of text from self. Currently, this function does not have a way to signal that the end of the file has been reached. For now, callers should check the result against B"". This will be fixed in a future release.

public static define read_to_string(path: String): String

Convenience method for reading a whole file into a String.

This opens the file named path, reads all content into a String, then closes the file.

public define write(data: A)

Attempt to write the contents of data to the file provided.

public static define write_to_path(path: String, data: A)

Convenience function for writing data to a file.

This opens the file named path, writes data to it, then closes the file.