当前位置:网站首页>The most comprehensive UE4 file operation in history, including opening, reading, writing, adding, deleting, modifying and checking
The most comprehensive UE4 file operation in history, including opening, reading, writing, adding, deleting, modifying and checking
2022-07-25 10:37:00 【Flying Pig】
One 、 Pre operation
Create a C++ project , And create a C++ Blue library function , And add header file
#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"

Two 、 Open file
UFUNCTION(BlueprintCallable, DisplayName = "OpenFile", Category = "File")
static TArray<FString> OpenFile();TArray<FString> UGenericArrayLibrary::OpenFile()
{
TArray<FString> FilePath; // Choose the file path
FString fileType = TEXT("*.*"); // Filter file types
FString defaultPath = FPaths::ConvertRelativePathToFull(FPaths::ProjectDir()); // The default opening path of the file selection window
IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();
bool bSuccess = DesktopPlatform->OpenFileDialog(nullptr, TEXT(" Open file "), defaultPath, TEXT(""), *fileType, EFileDialogFlags::None, FilePath);
for (auto& name : FilePath)
{
UE_LOG(LogTemp, Warning,
TEXT("%s"), *name);
}
if (bSuccess)
{
// File selection succeeded , File path path
UE_LOG(LogTemp,Warning,TEXT("Success"));
}
return FilePath;
}
3、 ... and 、 Read and write files ( character string )
// Read the file ( character string )
UFUNCTION(BlueprintCallable, DisplayName = "ReadFile", Category = "File")
static FString ReadFile(FString path);
// write file ( character string )
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;
}
Four 、 Read and write character arrays
/ Read the file ( A character array )
UFUNCTION(BlueprintCallable, DisplayName = "ReadFileArray", Category = "File")
static TArray<FString> ReadFileArray(FString path);
// write file ( A character array )
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);
}
5、 ... and 、 Get file path , Get the file name , Get file suffix
// Get the path of the file
UFUNCTION(BlueprintCallable, DisplayName = "Get FilePath", Category = "File")
static FString GetFilePath(FString path);
// Get the file name , Without the suffix
UFUNCTION(BlueprintCallable, DisplayName = "GetFileName", Category = "File")
static FString GetFileName(FString InPath, bool bRemovePath);
// Get file suffix
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);
}
6、 ... and 、 Add a folder And delete a folder
// Create a folder
UFUNCTION(BlueprintCallable, DisplayName = "CreateFolder", Category = "File")
static void CreatFolder(FString FolderName);
// Delete a folder
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);
}
7、 ... and 、 Move folder
// Moving files
UFUNCTION(BlueprintCallable, Category = "MoveFileTo")
static bool MoveFileTo(FString To, FString From);bool UGenericArrayLibrary::MoveFileTo(FString To, FString From)
{
return IFileManager::Get().Move(*To, *From);
}
8、 ... and 、 Find the folder
// Find all the files in the file directory
UFUNCTION(BlueprintCallable, DisplayName = "FindFolder", Category = "File")
static TArray<FString> FindFolder(FString Path, FString Filter, bool Files, bool Directory);
// Find all the files in the file directory. You can't delete and search
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;
}
边栏推荐
- 推荐系统-协同过滤在Spark中的实现
- MySQL offline deployment
- Angr(十)——官方文档(Part1)
- Number theory -- Research on divisor
- The ultimate summary of jsonobject parsing JSON format
- 搭建LNMP+DISCUZ论坛
- Storage, computing, distributed Virtualization (collection and sorting is suitable for Xiaobai)
- Install MySQL database version 5.7.29 under ubuntu20.04 system
- 10. Expect interaction free
- Oh my Zsh and TMUX configuration (personal)
猜你喜欢
随机推荐
Small knowledge of common classes
Trojang attack on neural networks paper reading notes
Bug classification and grading
Angr (III) - angr_ ctf
Test plan and test plan
Keras深度学习实战(16)——自编码器详解
Number theory --- the greatest common divisor and the least common multiple
Upgrade glibc 2.29 checking LD_ LIBRARY_ Path variable... Contains current directory error solution
8. SHELL file processing Three Musketeers sed
推荐系统-协同过滤在Spark中的实现
使用px2rem不生效
Pow(x,n)
Multithreading -- callable interface, lambda
Fastdfs离线部署(图文)
Voxceleb1 dataset Download
Install MySQL database version 5.7.29 under ubuntu20.04 system
Oh my Zsh and TMUX configuration (personal)
5. This simple "echo" usage, can the child next door!
3.信你能理解的!shell脚本之循环语句与函数,数组,冒泡排序
2、 What does the unittest framework do









