A clooection of static methods relating to File I/O.

This class contains a convenient handful of static methods to do common file operations. These methods abstract away the crude API’s of libc‘s stdio.h functions.

Public Static Functions

bool File::exists(String path)

Check if a file exists.

Determine if a file exists at a given path.

true if file exists, falseotherwise
  • path -

    The file’s path (relative or absolute)

size_t File::size(String path)

Return a file’s size in bytes.

This is a short hand method, that uses stdlib routines to get a file’s size. You provide the path to the file.

If the size cannot be determined (file does not exists), this method will return 0.

The file size in bytes or 0 on error (or empty file)
  • path -

    The relative or absolute path to the file

mono::String File::readFirstLine(String path, int maxLength, char endLineDelimiter)

Read and return the first line in a text file.

Read the first line of text in the file, defined by the given path. It is very important the file is a text file! This method will (by default) search for the first occurence of a newline character. All bytes before the newline is returned. (This means the newline character itself is not part of the returned.)

MEMORY USAGE: Be aware that the returned string is contained in memory. Be causious not to exhaust all RAM, by loading in a lot of text. Limit the maximum memory used by setting the optional maxLenght argument.

If the file does not exist, an empty string is returned.

A string with the first line of text
  • path -

    The path to the text file

  • maxLength -

    OPTIONAL: Set to a positive integer, to load only that many bytes.

  • endLineDelimiter -

    OPTIONAL: You can provide a custom stop delimiter, if you so not want .

bool File::writeString(String text, String path)

Write a string to a file (detructive)

You provide a text string, that is written to a provided file path. Any exists file at the path gets overwritten. The complete content of the string is written.

true upon success, falseotherwise
  • text -

    The string data to write to a file

  • path -

    The file’s path

bool File::appendString(String text, String path)

Append a text string to the end of a file.

Write a text string to a file (appending), without overwriting any text in the file. Only characters present in the string are written to the file. This method does not automatically insert newlines. If you want to append a line of text see appendLine

If the file does not exist, it is created and the text is written to the beginning of the file.

true on success. false otherwise.
  • text -

    The text string to append to the file

  • path -

    The path to the file that is appended

bool File::appendLine(String text, String path, const char *lineDelimiter)

Append a line of text to the end of a file.

Append a text line (string and a newline character), to the end of the defined file. By default the newline sequence is \\n, but you can overwrite this behavior using the third optional argument.

This method ensure that a subsequent string written to the file, will begin on a new line. If the provided text string already ends with a newline sequence, the outline will be 2 lines skipped.

If the destination file does not exist, it is created.

true on success, false otherwise
  • text -

    The text string to write to the file

  • path -

    The file destination path

  • lineDelimiter -

    Optional: Define a custom new line sequence