UE4 C++ 文件操作
作者:互联网
UE4 C++ 文件操作
文章主要是讲一些关于虚幻4中对于文件的一些操作,因为我还是一名UE4的学徒所以文章有什么错误欢迎留言指正,文章会持续更新。
创建文件夹
源码
/** Creates a directory. */
virtual bool MakeDirectory( const TCHAR* Path, bool Tree=0 )=0;
使用
//CurrentDirectory 新文件夹的目录
//NewFolderName 新文件夹名
FString NewFolderPath = FPaths::Combine(CurrentDirectory, NewFolderName);
bool bResult = IFileManager::Get().MakeDirectory(*NewFolderPath);
查找文件
源码
/** Finds file or directories. */
virtual void FindFiles( TArray<FString>& FileNames, const TCHAR* Filename, bool Files, bool Directories)=0;
/**
* Finds all the files within the given directory, with optional file extension filter.
*
* @param Directory, the absolute path to the directory to search. Ex: "C:\UE4\Pictures"
*
* @param FileExtension, If FileExtension is NULL, or an empty string "" then all files are found.
* Otherwise FileExtension can be of the form .EXT or just EXT and only files with that extension will be returned.
*
* @return FoundFiles, All the files that matched the optional FileExtension filter, or all files if none was specified.
*/
virtual void FindFiles(TArray<FString>& FoundFiles, const TCHAR* Directory, const TCHAR* FileExtension = nullptr) = 0;
/** Finds file or directories recursively. */
virtual void FindFilesRecursive( TArray<FString>& FileNames, const TCHAR* StartDirectory, const TCHAR* Filename, bool Files, bool Directories, bool bClearFileNames=true) = 0; // utility
使用
//两个bool值,第一个查找文件,第二个查找文件夹
//目录路径,查找文件夹需要添加通配符"*"
FString DirectoryPath = FPaths::Combine(CurrentDirectory, TEXT("*"));
TArray<FString> FileNames;
IFileManager::Get().FindFiles(FileNames, *DirectoryPath, false, true);
//查找目录下所有指定扩展名的文件
FString Extension = TEXT("*.json");
TArray<FString> FileNames;
IFileManager::Get().FindFiles(FileNames, *DirectoryPath, *Extension);
//递归查找文件(包括子文件夹)
//三个bool值,第一个查找文件,第二个查找文件夹,第三个完整的文件名(带扩展名)
FString Extension = TEXT("*.dat");
TArray<FString> FindedFiles;
IFileManager::Get().FindFilesRecursive(FindedFiles, *DirectoryPath, *Extension, true, false, true);
写入文件
源码
/**
* Write the FString to a file.
* Supports all combination of ANSI/Unicode files and platforms.
*/
static bool SaveStringToFile( const FString& String, const TCHAR* Filename, EEncodingOptions EncodingOptions = EEncodingOptions::AutoDetect, IFileManager* FileManager = &IFileManager::Get(), uint32 WriteFlags = 0 );
/**
* Write the FString to a file.
* Supports all combination of ANSI/Unicode files and platforms.
*/
static bool SaveStringArrayToFile( const TArray<FString>& Lines, const TCHAR* Filename, EEncodingOptions EncodingOptions = EEncodingOptions::AutoDetect, IFileManager* FileManager = &IFileManager::Get(), uint32 WriteFlags = 0 );
使用
//需要完整的文件路径
FString FilePath = FPaths::Combine(FolderPath, TEXT("123.json"));
bool bResult = FFileHelper::SaveStringToFile(WriteContent, *FilePath);
return bResult;
复制文件
源码
/** Copies a file. */
virtual uint32 Copy( const TCHAR* Dest, const TCHAR* Src, bool Replace=1, bool EvenIfReadOnly=0, bool Attributes=0, FCopyProgress* Progress = nullptr, EFileRead ReadFlags=FILEREAD_None, EFileWrite WriteFlags=FILEWRITE_None)=0; // utility
使用
//需要传入完整目标文件路径(带扩展名)
IFileManager::Get().Copy(*TargetFilePath, *SourceFilePath);
移动文件
源码
/** Moves/renames a file. */
virtual bool Move( const TCHAR* Dest, const TCHAR* Src, bool Replace=1, bool EvenIfReadOnly=0, bool Attributes=0, bool bDoNotRetryOrError=0 )=0;
使用
//需要传入完整目标文件路径(带扩展名)
bool bResult = IFileManager::Get().Move(*TargetFilePath, *SourceFilePath);
return bResult;
删除文件
源码
/** Deletes a file. */
virtual bool Delete( const TCHAR* Filename, bool RequireExists=0, bool EvenReadOnly=0, bool Quiet=0 )=0;
使用
//需要传入完整目标文件路径(带文件扩展名)
bool bResult = IFileManager::Get().Delete(*FilePath);
return bResult;
删除文件夹
源码
/** Deletes a directory. */
virtual bool DeleteDirectory( const TCHAR* Path, bool RequireExists=0, bool Tree=0 )=0;
使用
//需要删除子文件夹,最后一个参数为TRUE
bool bResult = IFileManager::Get().DeleteDirectory(*FolderPath, false, true);
return bResult;
标签:文件,bResult,const,Get,IFileManager,TCHAR,C++,bool,UE4 来源: https://blog.csdn.net/Kg_St/article/details/110958673