KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
FileUtil Class Reference

FileUtil. More...

#include <fileutil.h>

Static Public Member Functions

static QString getMD5String (const QString &filename)
 Compute the MD5 hash of a file as a hex string.
 
static QByteArray getMD5Bytes (const QString &filename)
 Compute the MD5 hash of a file as raw bytes.
 
static bool readAllBytes (const QString &filename, QByteArray &data)
 Read the entire contents of a file into a byte array.
 
static bool readAllLines (const QString &filename, QStringList &data, QStringConverter::Encoding encoding=QStringConverter::Utf8)
 Read all lines of a text file into a string list.
 
static bool writeAllBytes (const QString &filename, const QByteArray &data)
 Write a byte array to a file, overwriting any existing content.
 
static bool writeAllBytes (const QString &filename, const QByteArray &data, QStringConverter::Encoding encoding)
 Write a byte array to a file using a specific text encoding.
 
static bool writeAllLines (const QString &filename, const QStringList &lines)
 Write a list of strings to a file, one per line.
 
static bool appendAllBytes (const QString &filename, const QByteArray &data)
 Append a byte array to an existing file (or create if absent).
 
static int lineCount (const QString &filename)
 Count the number of lines in a text file.
 
static bool exists (const QString &filename)
 Test whether a file exists.
 
static bool remove (const QString &filename)
 Remove (delete) a file.
 
static bool touch (const QString &filename)
 Create an empty file or update its modification time if it exists.
 
static bool move (const QString &source, const QString &destination)
 Move (rename) a file from one path to another.
 
static bool moveToDirectory (const QString &sourceFilename, const QString &destinationDirectory)
 Move a file into a destination directory, preserving its basename.
 
static bool setModifyTime (const QString &filename, const QDateTime &value)
 Set the modification timestamp of a file.
 
static QString getTempFilename ()
 Generate a unique temporary file path.
 

Detailed Description

FileUtil.

Some static helper methods for one line file reads / writes and other file manipulations.

Stephen Punak, October 02 2020

Static helper methods for common file I/O operations.

Definition at line 20 of file fileutil.h.

Member Function Documentation

◆ appendAllBytes()

static bool FileUtil::appendAllBytes ( const QString &  filename,
const QByteArray &  data 
)
static

Append a byte array to an existing file (or create if absent).

Parameters
filenamePath to the file
dataBytes to append
Returns
true on success, false on error

◆ exists()

static bool FileUtil::exists ( const QString &  filename)
static

Test whether a file exists.

Parameters
filenamePath to the file
Returns
true if the file exists

◆ getMD5Bytes()

static QByteArray FileUtil::getMD5Bytes ( const QString &  filename)
static

Compute the MD5 hash of a file as raw bytes.

Parameters
filenamePath to the file
Returns
Raw MD5 hash bytes, or empty array on error

◆ getMD5String()

static QString FileUtil::getMD5String ( const QString &  filename)
static

Compute the MD5 hash of a file as a hex string.

Parameters
filenamePath to the file
Returns
Hex-encoded MD5 string, or empty string on error

◆ getTempFilename()

static QString FileUtil::getTempFilename ( )
static

Generate a unique temporary file path.

Returns
Path string for a new temporary file

◆ lineCount()

static int FileUtil::lineCount ( const QString &  filename)
static

Count the number of lines in a text file.

Parameters
filenamePath to the file
Returns
Number of lines, or -1 on error

◆ move()

static bool FileUtil::move ( const QString &  source,
const QString &  destination 
)
static

Move (rename) a file from one path to another.

Parameters
sourcePath to the source file
destinationPath to the destination
Returns
true on success, false on error

◆ moveToDirectory()

static bool FileUtil::moveToDirectory ( const QString &  sourceFilename,
const QString &  destinationDirectory 
)
static

Move a file into a destination directory, preserving its basename.

Parameters
sourceFilenamePath to the source file
destinationDirectoryPath to the target directory
Returns
true on success, false on error

◆ readAllBytes()

static bool FileUtil::readAllBytes ( const QString &  filename,
QByteArray &  data 
)
static

Read the entire contents of a file into a byte array.

Parameters
filenamePath to the file
dataOutput byte array receiving file contents
Returns
true on success, false on error

◆ readAllLines()

static bool FileUtil::readAllLines ( const QString &  filename,
QStringList &  data,
QStringConverter::Encoding  encoding = QStringConverter::Utf8 
)
static

Read all lines of a text file into a string list.

Parameters
filenamePath to the file
dataOutput string list receiving the lines
encodingText encoding to use (default UTF-8)
Returns
true on success, false on error

◆ remove()

static bool FileUtil::remove ( const QString &  filename)
static

Remove (delete) a file.

Parameters
filenamePath to the file to remove
Returns
true on success, false on error

◆ setModifyTime()

static bool FileUtil::setModifyTime ( const QString &  filename,
const QDateTime &  value 
)
static

Set the modification timestamp of a file.

Parameters
filenamePath to the file
valueNew modification timestamp
Returns
true on success, false on error

◆ touch()

static bool FileUtil::touch ( const QString &  filename)
static

Create an empty file or update its modification time if it exists.

Parameters
filenamePath to the file
Returns
true on success, false on error

◆ writeAllBytes() [1/2]

static bool FileUtil::writeAllBytes ( const QString &  filename,
const QByteArray &  data 
)
static

Write a byte array to a file, overwriting any existing content.

Parameters
filenamePath to the output file
dataBytes to write
Returns
true on success, false on error

◆ writeAllBytes() [2/2]

static bool FileUtil::writeAllBytes ( const QString &  filename,
const QByteArray &  data,
QStringConverter::Encoding  encoding 
)
static

Write a byte array to a file using a specific text encoding.

Parameters
filenamePath to the output file
dataBytes to write
encodingText encoding used when writing
Returns
true on success, false on error

◆ writeAllLines()

static bool FileUtil::writeAllLines ( const QString &  filename,
const QStringList &  lines 
)
static

Write a list of strings to a file, one per line.

Parameters
filenamePath to the output file
linesLines to write
Returns
true on success, false on error

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