ASL
File Class Reference

Detailed Description

Class File represents a file in the filesystem.

It can be used to get information about a file (its size, its modification date, whether it exists), to read or write in it in one step, without explicitly opening it, or to read and write in a more conventional way. The file is automatically closed on object destruction. For text files you should use class TextFile.

Date lasttime = File("access.log").lastModified();
ByteArray data = File("data.bin").content();
long long size = File("video.mp4").size();
File()
Constructs a File object with no associated file.
Definition: File.h:68
Long size() const
Returns the file size.
Array< byte > ByteArray
An alias for Array<byte>
Definition: Array.h:615

There are also conventional functions to explicitly open a file for reading or writing:

File file("data.bin", File::WRITE); // opened in constructor
file.seek(16);
file.write(buffer, sizeof(buffer)); // closed in destructor

#include <File.h>

Inheritance diagram for File:
TextFile

Public Member Functions

 File ()
 Constructs a File object with no associated file.
 
 File (const String &name)
 Constructs a File object with the given path name but does not open it.
 
 File (const String &name, OpenMode mode)
 Constructs a File object with the given path name and opens it with the given access mode. More...
 
 operator bool () const
 Returns true if this object refers to an open file.
 
bool operator== (const File &f) const
 Returns true if this object does not refer to an open file.
 
Date lastModified () const
 Returns the file's last modification date.
 
Date creationDate () const
 Returns the file's creation date.
 
bool setLastModified (const Date &t)
 Sets the file's last modification date.
 
Long size () const
 Returns the file size.
 
String name () const
 Returns the file's name (without its directory)
 
String extension () const
 Returns the file's extension (what follows the last dot)
 
bool hasExtension (const String &exts) const
 Returns true if this file's extension is any of those given (separated by '|'), case-insensitively.
 
const Stringpath () const
 Returns the full path of the file.
 
String directory () const
 Returns the directory containing the file.
 
bool isDirectory () const
 Returns true if the 'file' path is a directory.
 
bool exists () const
 Returns true if the 'file' path exists (file or directory)
 
bool isFile () const
 Returns true if the 'file' path exists and is actually a file.
 
bool copy (const String &to)
 Copies this file to a new directory or name.
 
bool move (const String &to)
 Moves or renames this file to to which can be a full name or a destination directory.
 
bool remove ()
 Deletes the file.
 
ByteArray firstBytes (int n)
 Returns the first n bytes in the file.
 
ByteArray content ()
 Returns the binary content of the file as an array of bytes.
 
bool put (const ByteArray &data)
 Writes the binary content of the file from an array of bytes. More...
 
bool open (const String &name, OpenMode mode=READ)
 Opens the file with the specified access mode.
 
bool open (OpenMode mode=READ)
 Opens the file with the specified access mode.
 
void close ()
 Closes the file.
 
Long position ()
 Returns the current position in the file.
 
void seek (Long offset, SeekMode from=START)
 Moves the file pointer to the given position. More...
 
bool end ()
 Returns true if the file pointer reached the end of the file.
 
void flush ()
 Flushes the write buffers effectively writing data on disk.
 
int read (void *p, int n)
 Reads n bytes from the file into the buffer pointed to by p.
 
int write (const void *p, int n)
 Writes n bytes from the buffer pointed to by p into the file.
 
template<class T >
Fileoperator<< (const T &x)
 Writes variable x to the file respecting endianness in binary form.
 
template<class T >
Fileoperator>> (T &x)
 Reads variable x from the file respecting endianness in binary form.
 

Static Public Member Functions

static File temp (const String &ext=".tmp")
 Creates a temporary file with optional extension and returns its path.
 

Constructor & Destructor Documentation

◆ File()

File ( const String name,
OpenMode  mode 
)
inlineexplicit

Constructs a File object with the given path name and opens it with the given access mode.

Parameters
nameFile name optionally including path
modeAccess mode: READ, WRITE, APPEND, RW (read+write)

Member Function Documentation

◆ put()

bool put ( const ByteArray data)

Writes the binary content of the file from an array of bytes.

Returns false on failure

◆ seek()

void seek ( Long  offset,
SeekMode  from = START 
)

Moves the file pointer to the given position.

Parameters
offsetnew offset position in file
fromfrom where the offset is expressed: START (beginning of file), HERE (current position), END (file end)

The documentation for this class was generated from the following file: