一、前期操作
創(chuàng)建一個(gè)C++項(xiàng)目,并且創(chuàng)建一個(gè)C++藍(lán)圖庫(kù)函數(shù),并且加入頭文件
#include "HAL/PlatformFilemanager.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
#include "Developer/DesktopPlatform/Public/DesktopPlatformModule.h"
#include "Developer/DesktopPlatform/Public/IDesktopPlatform.h"
#include "Runtime/Core/Public/HAL/FileManagerGeneric.h"
?二、打開文件
UFUNCTION(BlueprintCallable, DisplayName = "OpenFile", Category = "File")
static TArray<FString> OpenFile();
TArray<FString> UGenericArrayLibrary::OpenFile()
{
TArray<FString> FilePath; //選擇文件路徑
FString fileType = TEXT("*.*"); //過濾文件類型
FString defaultPath = FPaths::ConvertRelativePathToFull(FPaths::ProjectDir()); //文件選擇窗口默認(rèn)開啟路徑
IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();
bool bSuccess = DesktopPlatform->OpenFileDialog(nullptr, TEXT("打開文件"), defaultPath, TEXT(""), *fileType, EFileDialogFlags::None, FilePath);
for (auto& name : FilePath)
{
UE_LOG(LogTemp, Warning,
TEXT("%s"), *name);
}
if (bSuccess)
{
//文件選擇成功,文件路徑 path
UE_LOG(LogTemp,Warning,TEXT("Success"));
}
return FilePath;
}
三、讀取和寫入文件(字符串)
//讀取文件(字符串)
UFUNCTION(BlueprintCallable, DisplayName = "ReadFile", Category = "File")
static FString ReadFile(FString path);
//寫入文件(字符串)
UFUNCTION(BlueprintCallable, DisplayName = "WriteFile", Category = "File")
static bool WriteFile(FString saveFile,FString path);
FString UGenericArrayLibrary::ReadFile(FString path)
{
FString resultString;
FFileHelper::LoadFileToString(resultString,*path);
return resultString;
}
///
bool UGenericArrayLibrary::WriteFile(FString saveFile,FString path)
{
bool success;
success = FFileHelper::SaveStringToFile(saveFile,*path);
return success;
}
?四、讀取和寫入字符數(shù)組
/讀取文件(字符數(shù)組)
UFUNCTION(BlueprintCallable, DisplayName = "ReadFileArray", Category = "File")
static TArray<FString> ReadFileArray(FString path);
//寫入文件(字符數(shù)組)
UFUNCTION(BlueprintCallable, DisplayName = "WriteFileArray", Category = "File")
static bool WriteFileArray(TArray<FString> saveFile,FString path);
TArray<FString> UGenericArrayLibrary::ReadFileArray(FString path)
{
TArray<FString> results;
FFileHelper::LoadFileToStringArray(results, *path);
return results;
}
bool UGenericArrayLibrary::WriteFileArray(TArray<FString> saveFile, FString path)
{
return FFileHelper::SaveStringArrayToFile(saveFile,*path);
}
五、 獲取文件路徑,獲取文件名,獲取文件后綴
//獲取文件所在路徑
UFUNCTION(BlueprintCallable, DisplayName = "Get FilePath", Category = "File")
static FString GetFilePath(FString path);
//獲取文件名,不帶后綴
UFUNCTION(BlueprintCallable, DisplayName = "GetFileName", Category = "File")
static FString GetFileName(FString InPath, bool bRemovePath);
//獲取文件后綴
UFUNCTION(BlueprintCallable, DisplayName = "GetFileExtension", Category = "File")
static FString GetFileExtension(FString InPath, bool bIncludeDot);
FString UGenericArrayLibrary::GetFilePath(FString path)
{
FString Result;
Result = FPaths::GetPath(*path);
return Result;
}
FString UGenericArrayLibrary::GetFileName(FString InPath, bool bRemovePath)
{
return FPaths::GetBaseFilename(*InPath,bRemovePath);
}
FString UGenericArrayLibrary::GetFileExtension(FString InPath, bool bIncludeDot)
{
return FPaths::GetExtension(*InPath,bIncludeDot);
}
六、增加一個(gè)文件夾?和刪除一個(gè)文件夾
//創(chuàng)建一個(gè)文件夾
UFUNCTION(BlueprintCallable, DisplayName = "CreateFolder", Category = "File")
static void CreatFolder(FString FolderName);
//刪除一個(gè)文件夾
UFUNCTION(BlueprintCallable, DisplayName = "DeleteFolder", Category = "File")
static void DeleteFolder(FString FolderName);
void UGenericArrayLibrary::CreatFolder(FString FolderName)
{
FString Path = FPaths::ProjectDir()/ *FolderName;
Path = FPaths::ConvertRelativePathToFull(*Path);
FPlatformFileManager::Get().GetPlatformFile().CreateDirectoryTree(*Path);
}
void UGenericArrayLibrary::DeleteFolder(FString FolderName)
{
FString Path = FPaths::ProjectDir() / *FolderName;
Path = FPaths::ConvertRelativePathToFull(*Path);
FPlatformFileManager::Get().Get().GetPlatformFile().DeleteDirectoryRecursively(*Path);
}
文章來源:http://www.zghlxwxcb.cn/news/detail-407210.html
?七、移動(dòng)文件夾
//移動(dòng)文件
UFUNCTION(BlueprintCallable, Category = "MoveFileTo")
static bool MoveFileTo(FString To, FString From);
bool UGenericArrayLibrary::MoveFileTo(FString To, FString From)
{
return IFileManager::Get().Move(*To, *From);
}
八、查找文件夾
//查找文件目錄下的所有文件
UFUNCTION(BlueprintCallable, DisplayName = "FindFolder", Category = "File")
static TArray<FString> FindFolder(FString Path, FString Filter, bool Files, bool Directory);
//查找文件目錄下所有文件無法刪選查找
UFUNCTION(BlueprintCallable, DisplayName = "GetFolderFiles", Category = "File")
static TArray<FString> GetFolderFiles(FString Path);
TArray<FString> UGenericArrayLibrary::FindFolder(FString Path, FString Filter, bool Files, bool Directory)
{
TArray<FString> FilePathList;
FilePathList.Empty();
FFileManagerGeneric::Get().FindFilesRecursive(FilePathList, *Path, *Filter, Files, Directory);
return FilePathList;
}
//
TArray<FString> UGenericArrayLibrary::GetFolderFiles(FString Path)
{
TArray<FString> Files;
FPaths::NormalizeDirectoryName(Path);
IFileManager& FileManager = IFileManager::Get();
FString FinalPath = Path / TEXT("*");
FileManager.FindFiles(Files, *FinalPath, true, true);
return Files;
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-407210.html
到了這里,關(guān)于史上最全面的UE4 文件操作,打開,讀、寫,增、刪、改、查的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!