Represents a local file or directory. More...
#include <juce_File.h>
Classes | |
struct | NaturalFileComparator |
Comparator for files. More... | |
Public Types | |
enum | TypesOfFileToFind { findDirectories = 1 , findFiles = 2 , findFilesAndDirectories = 3 , ignoreHiddenFiles = 4 } |
Used in file searching, to specify whether to return files, directories, or both. More... | |
enum class | FollowSymlinks { no , noCycles , yes } |
enum | SpecialLocationType { userHomeDirectory , userDocumentsDirectory , userDesktopDirectory , userMusicDirectory , userMoviesDirectory , userPicturesDirectory , userApplicationDataDirectory , commonApplicationDataDirectory , commonDocumentsDirectory , tempDirectory , currentExecutableFile , currentApplicationFile , invokedExecutableFile , hostApplicationPath , windowsSystemDirectory , globalApplicationsDirectory , globalApplicationsDirectoryX86 , windowsLocalAppData } |
A set of types of location that can be passed to the getSpecialLocation() method. More... | |
Public Member Functions | |
File ()=default | |
Creates an (invalid) file object. | |
File (const String &absolutePath) | |
Creates a file from an absolute path. | |
File (const File &) | |
Creates a copy of another file object. | |
~File ()=default | |
Destructor. | |
File & | operator= (const String &newAbsolutePath) |
Sets the file based on an absolute pathname. | |
File & | operator= (const File &otherFile) |
Copies from another file object. | |
File (File &&) noexcept | |
Move constructor. | |
File & | operator= (File &&) noexcept |
Move assignment operator. | |
bool | exists () const |
Checks whether the file actually exists. | |
bool | existsAsFile () const |
Checks whether the file exists and is a file rather than a directory. | |
bool | isDirectory () const |
Checks whether the file is a directory that exists. | |
bool | isRoot () const |
Checks whether the path of this file represents the root of a file system, irrespective of its existence. | |
int64 | getSize () const |
Returns the size of the file in bytes. | |
const String & | getFullPathName () const noexcept |
Returns the complete, absolute path of this file. | |
String | getFileName () const |
Returns the last section of the pathname. | |
String | getRelativePathFrom (const File &directoryToBeRelativeTo) const |
Creates a relative path that refers to a file relatively to a given directory. | |
String | getFileExtension () const |
Returns the file's extension. | |
bool | hasFileExtension (StringRef extensionToTest) const |
Checks whether the file has a given extension. | |
File | withFileExtension (StringRef newExtension) const |
Returns a version of this file with a different file extension. | |
String | getFileNameWithoutExtension () const |
Returns the last part of the filename, without its file extension. | |
int | hashCode () const |
Returns a 32-bit hash-code that identifies this file. | |
int64 | hashCode64 () const |
Returns a 64-bit hash-code that identifies this file. | |
File | getChildFile (StringRef relativeOrAbsolutePath) const |
Returns a file that represents a relative (or absolute) sub-path of the current one. | |
File | getSiblingFile (StringRef siblingFileName) const |
Returns a file which is in the same directory as this one. | |
File | getParentDirectory () const |
Returns the directory that contains this file or directory. | |
bool | isAChildOf (const File &potentialParentDirectory) const |
Checks whether a file is somewhere inside a directory. | |
File | getNonexistentChildFile (const String &prefix, const String &suffix, bool putNumbersInBrackets=true) const |
Chooses a filename relative to this one that doesn't already exist. | |
File | getNonexistentSibling (bool putNumbersInBrackets=true) const |
Chooses a filename for a sibling file to this one that doesn't already exist. | |
bool | operator== (const File &) const |
Compares the pathnames for two files. | |
bool | operator!= (const File &) const |
Compares the pathnames for two files. | |
bool | operator< (const File &) const |
Compares the pathnames for two files. | |
bool | operator> (const File &) const |
Compares the pathnames for two files. | |
bool | hasWriteAccess () const |
Checks whether a file can be created or written to. | |
bool | hasReadAccess () const |
Checks whether a file can be read. | |
bool | setReadOnly (bool shouldBeReadOnly, bool applyRecursively=false) const |
Changes the write-permission of a file or directory. | |
bool | setExecutePermission (bool shouldBeExecutable) const |
Changes the execute-permissions of a file. | |
bool | isHidden () const |
Returns true if this file is a hidden or system file. | |
uint64 | getFileIdentifier () const |
Returns a unique identifier for the file, if one is available. | |
Time | getLastModificationTime () const |
Returns the last modification time of this file. | |
Time | getLastAccessTime () const |
Returns the last time this file was accessed. | |
Time | getCreationTime () const |
Returns the time that this file was created. | |
bool | setLastModificationTime (Time newTime) const |
Changes the modification time for this file. | |
bool | setLastAccessTime (Time newTime) const |
Changes the last-access time for this file. | |
bool | setCreationTime (Time newTime) const |
Changes the creation date for this file. | |
String | getVersion () const |
If possible, this will try to create a version string for the given file. | |
Result | create () const |
Creates an empty file if it doesn't already exist. | |
Result | createDirectory () const |
Creates a new directory for this filename. | |
bool | deleteFile () const |
Deletes a file. | |
bool | deleteRecursively (bool followSymlinks=false) const |
Deletes a file or directory and all its subdirectories. | |
bool | moveToTrash () const |
Moves this file or folder to the trash. | |
bool | moveFileTo (const File &targetLocation) const |
Moves or renames a file. | |
bool | copyFileTo (const File &targetLocation) const |
Copies a file. | |
bool | replaceFileIn (const File &targetLocation) const |
Replaces a file. | |
bool | copyDirectoryTo (const File &newDirectory) const |
Copies a directory. | |
Array< File > | findChildFiles (int whatToLookFor, bool searchRecursively, const String &wildCardPattern="*", FollowSymlinks followSymlinks=FollowSymlinks::yes) const |
Searches this directory for files matching a wildcard pattern. | |
int | findChildFiles (Array< File > &results, int whatToLookFor, bool searchRecursively, const String &wildCardPattern="*", FollowSymlinks followSymlinks=FollowSymlinks::yes) const |
Searches inside a directory for files matching a wildcard pattern. | |
int | getNumberOfChildFiles (int whatToLookFor, const String &wildCardPattern="*") const |
Searches inside a directory and counts how many files match a wildcard pattern. | |
bool | containsSubDirectories () const |
Returns true if this file is a directory that contains one or more subdirectories. | |
std::unique_ptr< FileInputStream > | createInputStream () const |
Creates a stream to read from this file. | |
std::unique_ptr< FileOutputStream > | createOutputStream (size_t bufferSize=0x8000) const |
Creates a stream to write to this file. | |
bool | loadFileAsData (MemoryBlock &result) const |
Loads a file's contents into memory as a block of binary data. | |
String | loadFileAsString () const |
Reads a file into memory as a string. | |
void | readLines (StringArray &destLines) const |
Reads the contents of this file as text and splits it into lines, which are appended to the given StringArray. | |
bool | appendData (const void *dataToAppend, size_t numberOfBytes) const |
Appends a block of binary data to the end of the file. | |
bool | replaceWithData (const void *dataToWrite, size_t numberOfBytes) const |
Replaces this file's contents with a given block of data. | |
bool | appendText (const String &textToAppend, bool asUnicode=false, bool writeUnicodeHeaderBytes=false, const char *lineEndings="\r\n") const |
Appends a string to the end of the file. | |
bool | replaceWithText (const String &textToWrite, bool asUnicode=false, bool writeUnicodeHeaderBytes=false, const char *lineEndings="\r\n") const |
Replaces this file's contents with a given text string. | |
bool | hasIdenticalContentTo (const File &other) const |
Attempts to scan the contents of this file and compare it to another file, returning true if this is possible and they match byte-for-byte. | |
String | getVolumeLabel () const |
Finds the name of the drive on which this file lives. | |
int | getVolumeSerialNumber () const |
Returns the serial number of the volume on which this file lives. | |
int64 | getBytesFreeOnVolume () const |
Returns the number of bytes free on the drive that this file lives on. | |
int64 | getVolumeTotalSize () const |
Returns the total size of the drive that contains this file. | |
bool | isOnCDRomDrive () const |
Returns true if this file is on a CD or DVD drive. | |
bool | isOnHardDisk () const |
Returns true if this file is on a hard disk. | |
bool | isOnRemovableDrive () const |
Returns true if this file is on a removable disk drive. | |
bool | startAsProcess (const String ¶meters=String()) const |
Launches the file as a process. | |
void | revealToUser () const |
Opens Finder, Explorer, or whatever the OS uses, to show the user this file's location. | |
bool | setAsCurrentWorkingDirectory () const |
Sets the current working directory to be this file. | |
bool | createSymbolicLink (const File &linkFileToCreate, bool overwriteExisting) const |
Tries to create a symbolic link and returns a boolean to indicate success. | |
bool | isSymbolicLink () const |
Returns true if this file is a link or alias that can be followed using getLinkedTarget(). | |
File | getLinkedTarget () const |
If this file is a link or alias, this returns the file that it points to. | |
String | getNativeLinkedTarget () const |
This returns the native path that the symbolic link points to. | |
bool | createShortcut (const String &description, const File &linkFileToCreate) const |
Windows ONLY - Creates a win32 .LNK shortcut file that links to this file. | |
bool | isShortcut () const |
Windows ONLY - Returns true if this is a win32 .LNK file. | |
OSType | getMacOSType () const |
OSX ONLY - Finds the OSType of a file from the its resources. | |
bool | isBundle () const |
OSX ONLY - Returns true if this file is actually a bundle. | |
void | addToDock () const |
OSX ONLY - Adds this file to the OSX dock. | |
Static Public Member Functions | |
static String | descriptionOfSizeInBytes (int64 bytes) |
Utility function to convert a file size in bytes to a neat string description. | |
static void | findFileSystemRoots (Array< File > &results) |
Creates a set of files to represent each file root. | |
static File JUCE_CALLTYPE | getSpecialLocation (const SpecialLocationType type) |
Finds the location of a special type of file or directory, such as a home folder or documents folder. | |
static File | createTempFile (StringRef fileNameEnding) |
Returns a temporary file in the system's temp directory. | |
static File | getCurrentWorkingDirectory () |
Returns the current working directory. | |
static juce_wchar | getSeparatorChar () |
The system-specific file separator character. | |
static StringRef | getSeparatorString () |
The system-specific file separator character, as a string. | |
static String | createLegalFileName (const String &fileNameToFix) |
Returns a version of a filename with any illegal characters removed. | |
static String | createLegalPathName (const String &pathNameToFix) |
Returns a version of a path with any illegal characters removed. | |
static bool | areFileNamesCaseSensitive () |
Indicates whether filenames are case-sensitive on the current operating system. | |
static bool | isAbsolutePath (StringRef path) |
Returns true if the string seems to be a fully-specified absolute path. | |
static File | createFileWithoutCheckingPath (const String &absolutePath) noexcept |
Creates a file that simply contains this string, without doing the sanity-checking that the normal constructors do. | |
static String | addTrailingSeparator (const String &path) |
Adds a separator character to the end of a path if it doesn't already have one. | |
static bool | createSymbolicLink (const File &linkFileToCreate, const String &nativePathOfTarget, bool overwriteExisting) |
Create a symbolic link to a native path and return a boolean to indicate success. | |
static File | getContainerForSecurityApplicationGroupIdentifier (const String &appGroup) |
Returns the path to the container shared by all apps with the provided app group ID. | |
Represents a local file or directory.
This class encapsulates the absolute pathname of a file or directory, and has methods for finding out about the file and changing its properties.
To read or write to the file, there are methods for returning an input or output stream.
Used in file searching, to specify whether to return files, directories, or both.
|
strong |
A set of types of location that can be passed to the getSpecialLocation() method.
Enumerator | |
---|---|
userHomeDirectory | The user's home folder. This is the same as using File ("~"). |
userDocumentsDirectory | The user's default documents folder. On Windows, this might be the user's "My Documents" folder. On the Mac it'll be their "Documents" folder. Linux doesn't tend to have one of these, so it might just return their home folder. |
userDesktopDirectory | The folder that contains the user's desktop objects. |
userMusicDirectory | The most likely place where a user might store their music files. |
userMoviesDirectory | The most likely place where a user might store their movie files. |
userPicturesDirectory | The most likely place where a user might store their picture files. |
userApplicationDataDirectory | The folder in which applications store their persistent user-specific settings. On Windows, this might be "\Documents and Settings\username\Application Data". On the Mac, it might be "~/Library". If you're going to store your settings in here, always create your own sub-folder to put them in, to avoid making a mess. On GNU/Linux it is "~/.config" and you may need to create the directory before using it. |
commonApplicationDataDirectory | An equivalent of the userApplicationDataDirectory folder that is shared by all users of the computer, rather than just the current user. On the Mac it'll be "/Library", on Windows, it could be something like "\Documents and Settings\All Users\Application Data". On GNU/Linux it is "/opt". Depending on the setup, this folder may be read-only. |
commonDocumentsDirectory | A place to put documents which are shared by all users of the machine. On Windows this may be somewhere like "C:\Users\Public\Documents", on OSX it will be something like "/Users/Shared". Other OSes may have no such concept though, so be careful. |
tempDirectory | The folder that should be used for temporary files. Always delete them when you're finished, to keep the user's computer tidy! |
currentExecutableFile | Returns this application's executable file. If running as a plug-in or DLL, this will (where possible) be the DLL rather than the host app. On the mac this will return the unix binary, not the package folder - see currentApplicationFile for that. See also invokedExecutableFile, which is similar, but if the exe was launched from a file link, invokedExecutableFile will return the name of the link. |
currentApplicationFile | Returns this application's location. If running as a plug-in or DLL, this will (where possible) be the DLL rather than the host app. On the mac this will return the package folder (if it's in one), not the unix binary that's inside it - compare with currentExecutableFile. |
invokedExecutableFile | Returns the file that was invoked to launch this executable. This may differ from currentExecutableFile if the app was started from e.g. a link - this will return the name of the link that was used, whereas currentExecutableFile will return the actual location of the target executable. |
hostApplicationPath | In a plugin, this will return the path of the host executable. |
windowsSystemDirectory | On a Windows machine, returns the location of the Windows/System32 folder. |
globalApplicationsDirectory | The directory in which applications normally get installed. So on windows, this would be something like "C:\Program Files", on the Mac "/Applications", or "/usr" on linux. |
globalApplicationsDirectoryX86 | On a Windows machine, returns the directory in which 32 bit applications normally get installed. On a 64 bit machine this would be something like "C:\Program Files (x86)", whereas for 32 bit machines this would match globalApplicationsDirectory and be something like "C:\Program Files".
|
windowsLocalAppData | On a Windows machine returns the LOCALAPPDATA% folder. |
|
default |
Creates an (invalid) file object.
The file is initially set to an empty path, so getFullPathName() will return an empty string.
You can use its operator= method to point it at a proper file.
File::File | ( | const String & | absolutePath | ) |
Creates a file from an absolute path.
If the path supplied is a relative path, it is taken to be relative to the current working directory (see File::getCurrentWorkingDirectory()), but this isn't a recommended way of creating a file, because you never know what the CWD is going to be.
On the Mac/Linux, the path can include "~" notation for referring to user home directories.
File::File | ( | const File & | ) |
Creates a copy of another file object.
|
default |
Destructor.
|
noexcept |
Move constructor.
Sets the file based on an absolute pathname.
If the path supplied is a relative path, it is taken to be relative to the current working directory (see File::getCurrentWorkingDirectory()), but this isn't a recommended way of creating a file, because you never know what the CWD is going to be.
On the Mac/Linux, the path can include "~" notation for referring to user home directories.
bool File::exists | ( | ) | const |
Checks whether the file actually exists.
bool File::existsAsFile | ( | ) | const |
Checks whether the file exists and is a file rather than a directory.
bool File::isDirectory | ( | ) | const |
Checks whether the file is a directory that exists.
Referenced by File::NaturalFileComparator::compareElements().
bool File::isRoot | ( | ) | const |
Checks whether the path of this file represents the root of a file system, irrespective of its existence.
This will return true for "C:", "D:", etc on Windows and "/" on other platforms.
int64 File::getSize | ( | ) | const |
Returns the size of the file in bytes.
Utility function to convert a file size in bytes to a neat string description.
So for example 100 would return "100 bytes", 2000 would return "2 KB", 2000000 would produce "2 MB", etc.
|
noexcept |
Returns the complete, absolute path of this file.
This includes the filename and all its parent folders. On Windows it'll also include the drive letter prefix; on Mac or Linux it'll be a complete path starting from the root folder.
If you just want the file's name, you should use getFileName() or getFileNameWithoutExtension().
Referenced by File::NaturalFileComparator::compareElements(), PluginHostType::getHostPath(), and StandalonePluginHolder::setLastFile().
String File::getFileName | ( | ) | const |
Returns the last section of the pathname.
Returns just the final part of the path - e.g. if the whole path is "/moose/fish/foo.txt" this will return "foo.txt".
For a directory, it returns the final part of the path - e.g. for the directory "/moose/fish" it'll return "fish".
If the filename begins with a dot, it'll return the whole filename, e.g. for "/moose/.fish", it'll return ".fish"
Creates a relative path that refers to a file relatively to a given directory.
e.g. File ("/moose/foo.txt").getRelativePathFrom (File ("/moose/fish/haddock")) would return "../../foo.txt".
If it's not possible to navigate from one file to the other, an absolute path is returned. If the paths are invalid, an empty string may also be returned.
directoryToBeRelativeTo | the directory which the resultant string will be relative to. If this is actually a file rather than a directory, its parent directory will be used instead. If it doesn't exist, it's assumed to be a directory. |
String File::getFileExtension | ( | ) | const |
Returns the file's extension.
Returns the file extension of this file, also including the dot.
e.g. "/moose/fish/foo.txt" would return ".txt"
bool File::hasFileExtension | ( | StringRef | extensionToTest | ) | const |
Checks whether the file has a given extension.
extensionToTest | the extension to look for - it doesn't matter whether or not this string has a dot at the start, so ".wav" and "wav" will have the same effect. To compare with multiple extensions, this parameter can contain multiple strings, separated by semi-colons - so, for example: hasFileExtension (".jpeg;png;gif") would return true if the file has any of those three extensions. |
Returns a version of this file with a different file extension.
e.g. File ("/moose/fish/foo.txt").withFileExtension ("html") returns "/moose/fish/foo.html"
newExtension | the new extension, either with or without a dot at the start (this doesn't make any difference). To get remove a file's extension altogether, pass an empty string into this function. |
String File::getFileNameWithoutExtension | ( | ) | const |
Returns the last part of the filename, without its file extension.
e.g. for "/moose/fish/foo.txt" this will return "foo".
int File::hashCode | ( | ) | const |
Returns a 32-bit hash-code that identifies this file.
This is based on the filename. Obviously it's possible, although unlikely, that two files will have the same hash-code.
int64 File::hashCode64 | ( | ) | const |
Returns a 64-bit hash-code that identifies this file.
This is based on the filename. Obviously it's possible, although unlikely, that two files will have the same hash-code.
Returns a file that represents a relative (or absolute) sub-path of the current one.
This will find a child file or directory of the current object.
e.g. File ("/moose/fish").getChildFile ("foo.txt") will produce "/moose/fish/foo.txt". File ("/moose/fish").getChildFile ("haddock/foo.txt") will produce "/moose/fish/haddock/foo.txt". File ("/moose/fish").getChildFile ("../foo.txt") will produce "/moose/foo.txt".
If the string is actually an absolute path, it will be treated as such, e.g. File ("/moose/fish").getChildFile ("/foo.txt") will produce "/foo.txt"
Returns a file which is in the same directory as this one.
This is equivalent to getParentDirectory().getChildFile (name).
File File::getParentDirectory | ( | ) | const |
Returns the directory that contains this file or directory.
e.g. for "/moose/fish/foo.txt" this will return "/moose/fish".
If you are already at the root directory ("/" or "C:") then this method will return the root directory.
bool File::isAChildOf | ( | const File & | potentialParentDirectory | ) | const |
Checks whether a file is somewhere inside a directory.
Returns true if this file is somewhere inside a subdirectory of the directory that is passed in. Neither file actually has to exist, because the function just checks the paths for similarities.
e.g. File ("/moose/fish/foo.txt").isAChildOf ("/moose") is true. File ("/moose/fish/foo.txt").isAChildOf ("/moose/fish") is also true.
File File::getNonexistentChildFile | ( | const String & | prefix, |
const String & | suffix, | ||
bool | putNumbersInBrackets = true ) const |
Chooses a filename relative to this one that doesn't already exist.
If this file is a directory, this will return a child file of this directory that doesn't exist, by adding numbers to a prefix and suffix until it finds one that isn't already there.
If the prefix + the suffix doesn't exist, it won't bother adding a number.
e.g. File ("/moose/fish").getNonexistentChildFile ("foo", ".txt", true) might return "/moose/fish/foo(2).txt" if there's already a file called "foo.txt".
prefix | the string to use for the filename before the number |
suffix | the string to add to the filename after the number |
putNumbersInBrackets | if true, this will create filenames in the format "prefix(number)suffix", if false, it will leave the brackets out. |
File File::getNonexistentSibling | ( | bool | putNumbersInBrackets = true | ) | const |
Chooses a filename for a sibling file to this one that doesn't already exist.
If this file doesn't exist, this will just return itself, otherwise it will return an appropriate sibling that doesn't exist, e.g. if a file "/moose/fish/foo.txt" exists, this might return "/moose/fish/foo(2).txt".
putNumbersInBrackets | whether to add brackets around the numbers that get appended to the new filename. |
bool File::operator== | ( | const File & | ) | const |
Compares the pathnames for two files.
bool File::operator!= | ( | const File & | ) | const |
Compares the pathnames for two files.
bool File::operator< | ( | const File & | ) | const |
Compares the pathnames for two files.
bool File::operator> | ( | const File & | ) | const |
Compares the pathnames for two files.
bool File::hasWriteAccess | ( | ) | const |
Checks whether a file can be created or written to.
bool File::hasReadAccess | ( | ) | const |
Checks whether a file can be read.
bool File::setReadOnly | ( | bool | shouldBeReadOnly, |
bool | applyRecursively = false ) const |
Changes the write-permission of a file or directory.
shouldBeReadOnly | whether to add or remove write-permission |
applyRecursively | if the file is a directory and this is true, it will recurse through all the subfolders changing the permissions of all files |
bool File::setExecutePermission | ( | bool | shouldBeExecutable | ) | const |
Changes the execute-permissions of a file.
shouldBeExecutable | whether to add or remove execute-permission |
bool File::isHidden | ( | ) | const |
Returns true if this file is a hidden or system file.
The criteria for deciding whether a file is hidden are platform-dependent.
uint64 File::getFileIdentifier | ( | ) | const |
Returns a unique identifier for the file, if one is available.
Depending on the OS and file-system, this may be a unix inode number or a win32 file identifier, or 0 if it fails to find one. The number will be unique on the filesystem, but not globally.
Time File::getLastModificationTime | ( | ) | const |
Returns the last modification time of this file.
Time File::getLastAccessTime | ( | ) | const |
Returns the last time this file was accessed.
Time File::getCreationTime | ( | ) | const |
Returns the time that this file was created.
bool File::setLastModificationTime | ( | Time | newTime | ) | const |
Changes the modification time for this file.
newTime | the time to apply to the file |
bool File::setLastAccessTime | ( | Time | newTime | ) | const |
Changes the last-access time for this file.
newTime | the time to apply to the file |
bool File::setCreationTime | ( | Time | newTime | ) | const |
Changes the creation date for this file.
newTime | the time to apply to the file |
String File::getVersion | ( | ) | const |
If possible, this will try to create a version string for the given file.
The OS may be able to look at the file and give a version for it - e.g. with executables, bundles, dlls, etc. If no version is available, this will return an empty string.
Result File::create | ( | ) | const |
Creates an empty file if it doesn't already exist.
If the file that this object refers to doesn't exist, this will create a file of zero size.
If it already exists or is a directory, this method will do nothing.
If the parent directories of the File do not exist then this method will recursively create the parent directories.
Result File::createDirectory | ( | ) | const |
Creates a new directory for this filename.
This will try to create the file as a directory, and will also create any parent directories it needs in order to complete the operation.
bool File::deleteFile | ( | ) | const |
Deletes a file.
If this file is actually a directory, it may not be deleted correctly if it contains files. See deleteRecursively() as a better way of deleting directories.
If this file is a symlink, then the symlink will be deleted and not the target of the symlink.
bool File::deleteRecursively | ( | bool | followSymlinks = false | ) | const |
Deletes a file or directory and all its subdirectories.
If this file is a directory, this will try to delete it and all its subfolders. If it's just a file, it will just try to delete the file.
followSymlinks | If true, then any symlink pointing to a directory will also recursively delete the contents of that directory |
bool File::moveToTrash | ( | ) | const |
Moves this file or folder to the trash.
bool File::moveFileTo | ( | const File & | targetLocation | ) | const |
Moves or renames a file.
Tries to move a file to a different location. If the target file already exists, this will attempt to delete it first, and will fail if this can't be done.
Note that the destination file isn't the directory to put it in, it's the actual filename that you want the new file to have.
Also note that on some OSes (e.g. Windows), moving files between different volumes may not be possible.
This function will often fail to move directories because of the ambiguities about merging existing directories. Use copyDirectoryTo() and deleteRecursively() in these situations.
bool File::copyFileTo | ( | const File & | targetLocation | ) | const |
Copies a file.
Tries to copy a file to a different location. If the target file already exists, this will attempt to delete it first, and will fail if this can't be done.
Note that the target file isn't the directory to put it in, it's the actual filename that you want the new file to have.
bool File::replaceFileIn | ( | const File & | targetLocation | ) | const |
Replaces a file.
Replace the file in the given location, assuming the replaced files identity. Depending on the file system this will preserve file attributes such as creation date, short file name, etc.
If replacement succeeds the original file is deleted.
bool File::copyDirectoryTo | ( | const File & | newDirectory | ) | const |
Copies a directory.
Tries to copy an entire directory, recursively.
If this file isn't a directory or if any target files can't be created, this will return false.
newDirectory | the directory that this one should be copied to. Note that this is the name of the actual directory to create, not the directory into which the new one should be placed, so there must be enough write privileges to create it if it doesn't exist. Any files inside it will be overwritten by similarly named ones that are copied. |
Array< File > File::findChildFiles | ( | int | whatToLookFor, |
bool | searchRecursively, | ||
const String & | wildCardPattern = "*", | ||
FollowSymlinks | followSymlinks = FollowSymlinks::yes ) const |
Searches this directory for files matching a wildcard pattern.
Assuming that this file is a directory, this method will search it for either files or subdirectories whose names match a filename pattern. Note that the order in which files are returned is completely undefined!
whatToLookFor | a value from the TypesOfFileToFind enum, specifying whether to return files, directories, or both. If the ignoreHiddenFiles flag is also added to this value, hidden files won't be returned |
searchRecursively | if true, all subdirectories will be recursed into to do an exhaustive search |
wildCardPattern | the filename pattern to search for, e.g. "*.txt" |
followSymlinks | the method that should be used to handle symbolic links |
int File::findChildFiles | ( | Array< File > & | results, |
int | whatToLookFor, | ||
bool | searchRecursively, | ||
const String & | wildCardPattern = "*", | ||
FollowSymlinks | followSymlinks = FollowSymlinks::yes ) const |
Searches inside a directory for files matching a wildcard pattern.
Note that there's a newer, better version of this method which returns the results array, and in almost all cases, you should use that one instead! This one is kept around mainly for legacy code to use.
int File::getNumberOfChildFiles | ( | int | whatToLookFor, |
const String & | wildCardPattern = "*" ) const |
Searches inside a directory and counts how many files match a wildcard pattern.
Assuming that this file is a directory, this method will search it for either files or subdirectories whose names match a filename pattern, and will return the number of matches found.
This isn't a recursive call, and will only search this directory, not its children.
whatToLookFor | a value from the TypesOfFileToFind enum, specifying whether to count files, directories, or both. If the ignoreHiddenFiles flag is also added to this value, hidden files won't be counted |
wildCardPattern | the filename pattern to search for, e.g. "*.txt" |
bool File::containsSubDirectories | ( | ) | const |
Returns true if this file is a directory that contains one or more subdirectories.
std::unique_ptr< FileInputStream > File::createInputStream | ( | ) | const |
Creates a stream to read from this file.
Note that this is an old method, and actually it's usually best to avoid it and instead use an RAII pattern with an FileInputStream directly, e.g.
std::unique_ptr< FileOutputStream > File::createOutputStream | ( | size_t | bufferSize = 0x8000 | ) | const |
Creates a stream to write to this file.
Note that this is an old method, and actually it's usually best to avoid it and instead use an RAII pattern with an FileOutputStream directly, e.g.
If the file exists, the stream that is returned will be positioned ready for writing at the end of the file. If you want to write to the start of the file, replacing the existing content, then you can do the following:
bool File::loadFileAsData | ( | MemoryBlock & | result | ) | const |
Loads a file's contents into memory as a block of binary data.
Of course, trying to load a very large file into memory will blow up, so it's better to check first.
result | the data block to which the file's contents should be appended - note that if the memory block might already contain some data, you might want to clear it first |
Referenced by StandalonePluginHolder::askUserToLoadState().
String File::loadFileAsString | ( | ) | const |
Reads a file into memory as a string.
Attempts to load the entire file as a zero-terminated string.
This makes use of InputStream::readEntireStreamAsString, which can read either UTF-16 or UTF-8 file formats.
void File::readLines | ( | StringArray & | destLines | ) | const |
Reads the contents of this file as text and splits it into lines, which are appended to the given StringArray.
bool File::appendData | ( | const void * | dataToAppend, |
size_t | numberOfBytes ) const |
Appends a block of binary data to the end of the file.
This will try to write the given buffer to the end of the file.
bool File::replaceWithData | ( | const void * | dataToWrite, |
size_t | numberOfBytes ) const |
Replaces this file's contents with a given block of data.
This will delete the file and replace it with the given data.
A nice feature of this method is that it's safe - instead of deleting the file first and then re-writing it, it creates a new temporary file, writes the data to that, and then moves the new file to replace the existing file. This means that if the power gets pulled out or something crashes, you're a lot less likely to end up with a corrupted or unfinished file..
Returns true if the operation succeeds, or false if it fails.
Referenced by StandalonePluginHolder::askUserToSaveState().
bool File::appendText | ( | const String & | textToAppend, |
bool | asUnicode = false, | ||
bool | writeUnicodeHeaderBytes = false, | ||
const char * | lineEndings = "\r\n" ) const |
Appends a string to the end of the file.
This will try to append a text string to the file, as either 16-bit unicode or 8-bit characters in the default system encoding.
It can also write the 'ff fe' unicode header bytes before the text to indicate the endianness of the file.
If lineEndings is nullptr, then line endings in the text won't be modified. If you pass "\\n" or "\\r\\n" then this function will replace any existing line feeds.
bool File::replaceWithText | ( | const String & | textToWrite, |
bool | asUnicode = false, | ||
bool | writeUnicodeHeaderBytes = false, | ||
const char * | lineEndings = "\r\n" ) const |
Replaces this file's contents with a given text string.
This will delete the file and replace it with the given text.
A nice feature of this method is that it's safe - instead of deleting the file first and then re-writing it, it creates a new temporary file, writes the text to that, and then moves the new file to replace the existing file. This means that if the power gets pulled out or something crashes, you're a lot less likely to end up with an empty file..
For an explanation of the parameters here, see the appendText() method.
Returns true if the operation succeeds, or false if it fails.
bool File::hasIdenticalContentTo | ( | const File & | other | ) | const |
Attempts to scan the contents of this file and compare it to another file, returning true if this is possible and they match byte-for-byte.
Creates a set of files to represent each file root.
e.g. on Windows this will create files for "c:\", "d:" etc according to which ones are available. On the Mac/Linux, this will probably just add a single entry for "/".
String File::getVolumeLabel | ( | ) | const |
Finds the name of the drive on which this file lives.
int File::getVolumeSerialNumber | ( | ) | const |
Returns the serial number of the volume on which this file lives.
int64 File::getBytesFreeOnVolume | ( | ) | const |
Returns the number of bytes free on the drive that this file lives on.
int64 File::getVolumeTotalSize | ( | ) | const |
Returns the total size of the drive that contains this file.
bool File::isOnCDRomDrive | ( | ) | const |
Returns true if this file is on a CD or DVD drive.
bool File::isOnHardDisk | ( | ) | const |
Returns true if this file is on a hard disk.
This will fail if it's a network drive, but will still be true for removable hard-disks.
bool File::isOnRemovableDrive | ( | ) | const |
Returns true if this file is on a removable disk drive.
This might be a usb-drive, a CD-rom, or maybe a network drive.
Launches the file as a process.
void File::revealToUser | ( | ) | const |
Opens Finder, Explorer, or whatever the OS uses, to show the user this file's location.
|
static |
Finds the location of a special type of file or directory, such as a home folder or documents folder.
Referenced by PluginHostType::getHostPath(), and StandalonePluginHolder::getLastFile().
Returns a temporary file in the system's temp directory.
This will try to return the name of a non-existent temp file. To get the temp folder, you can use getSpecialLocation (File::tempDirectory).
|
static |
Returns the current working directory.
bool File::setAsCurrentWorkingDirectory | ( | ) | const |
Sets the current working directory to be this file.
For this to work the file must point to a valid directory.
|
static |
The system-specific file separator character.
On Windows, this will be '\', on Mac/Linux, it'll be '/'
|
static |
The system-specific file separator character, as a string.
On Windows, this will be '\', on Mac/Linux, it'll be '/'
Returns a version of a filename with any illegal characters removed.
This will return a copy of the given string after removing characters that are not allowed in a legal filename, and possibly shortening the string if it's too long.
Because this will remove slashes, don't use it on an absolute pathname - use createLegalPathName() for that.
Returns a version of a path with any illegal characters removed.
Similar to createLegalFileName(), but this won't remove slashes, so can be used on a complete pathname.
|
static |
Indicates whether filenames are case-sensitive on the current operating system.
|
static |
Returns true if the string seems to be a fully-specified absolute path.
Creates a file that simply contains this string, without doing the sanity-checking that the normal constructors do.
Best to avoid this unless you really know what you're doing.
Adds a separator character to the end of a path if it doesn't already have one.
bool File::createSymbolicLink | ( | const File & | linkFileToCreate, |
bool | overwriteExisting ) const |
Tries to create a symbolic link and returns a boolean to indicate success.
bool File::isSymbolicLink | ( | ) | const |
Returns true if this file is a link or alias that can be followed using getLinkedTarget().
File File::getLinkedTarget | ( | ) | const |
If this file is a link or alias, this returns the file that it points to.
If the file isn't actually a link, it'll just return itself.
|
static |
Create a symbolic link to a native path and return a boolean to indicate success.
Use this method if you want to create a link to a relative path or a special native file path (such as a device file on Windows).
String File::getNativeLinkedTarget | ( | ) | const |
This returns the native path that the symbolic link points to.
The returned path is a native path of the current OS and can be a relative, absolute or special path.
Windows ONLY - Creates a win32 .LNK shortcut file that links to this file.
bool File::isShortcut | ( | ) | const |
Windows ONLY - Returns true if this is a win32 .LNK file.
OSType File::getMacOSType | ( | ) | const |
OSX ONLY - Finds the OSType of a file from the its resources.
bool File::isBundle | ( | ) | const |
OSX ONLY - Returns true if this file is actually a bundle.
void File::addToDock | ( | ) | const |
OSX ONLY - Adds this file to the OSX dock.
|
static |
Returns the path to the container shared by all apps with the provided app group ID.
You must pass one of the app group IDs listed in your app's entitlements file.
On failure, this function may return a non-existent file, so you should check that the path exists and is writable before trying to use it.