史上最全面的UE4 文件操作,打开,读、写,增、删、改、查

一、前期操作

创建一个C++项目,并且创建一个C++蓝图库函数,并且加入头文件

#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"

史上最全面的UE4 文件操作,打开,读、写,增、删、改、查_第1张图片

 二、打开文件

UFUNCTION(BlueprintCallable, DisplayName = "OpenFile", Category = "File")
	   static TArray OpenFile();
TArray UGenericArrayLibrary::OpenFile()
{
	TArray FilePath; //选择文件路径
	FString fileType = TEXT("*.*"); //过滤文件类型
	FString defaultPath = FPaths::ConvertRelativePathToFull(FPaths::ProjectDir()); //文件选择窗口默认开启路径
	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;
}

史上最全面的UE4 文件操作,打开,读、写,增、删、改、查_第2张图片
三、读取和写入文件(字符串)

//读取文件(字符串)
	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;
}

史上最全面的UE4 文件操作,打开,读、写,增、删、改、查_第3张图片

 四、读取和写入字符数组

/读取文件(字符数组)
	UFUNCTION(BlueprintCallable, DisplayName = "ReadFileArray", Category = "File")
		static TArray ReadFileArray(FString path);
	//写入文件(字符数组)
	UFUNCTION(BlueprintCallable, DisplayName = "WriteFileArray", Category = "File")
		static bool WriteFileArray(TArray saveFile,FString path);
TArray UGenericArrayLibrary::ReadFileArray(FString path)
{
	TArray results;
	FFileHelper::LoadFileToStringArray(results, *path);
	return results;
}

bool UGenericArrayLibrary::WriteFileArray(TArray saveFile, FString path)
{
	return FFileHelper::SaveStringArrayToFile(saveFile,*path);
}

史上最全面的UE4 文件操作,打开,读、写,增、删、改、查_第4张图片

五、 获取文件路径,获取文件名,获取文件后缀

	//获取文件所在路径
	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);
}

史上最全面的UE4 文件操作,打开,读、写,增、删、改、查_第5张图片

六、增加一个文件夹 和删除一个文件夹

//创建一个文件夹
	UFUNCTION(BlueprintCallable, DisplayName = "CreateFolder", Category = "File")
		static void CreatFolder(FString FolderName);
//删除一个文件夹
	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);
}

史上最全面的UE4 文件操作,打开,读、写,增、删、改、查_第6张图片

 七、移动文件夹

//移动文件
	UFUNCTION(BlueprintCallable, Category = "MoveFileTo")
		static bool MoveFileTo(FString To, FString From);
bool UGenericArrayLibrary::MoveFileTo(FString To, FString From)
{

	return IFileManager::Get().Move(*To, *From);
}

史上最全面的UE4 文件操作,打开,读、写,增、删、改、查_第7张图片
八、查找文件夹

 

//查找文件目录下的所有文件
	UFUNCTION(BlueprintCallable, DisplayName = "FindFolder", Category = "File")
		static TArray FindFolder(FString Path, FString Filter, bool Files, bool Directory);
	//查找文件目录下所有文件无法删选查找
	UFUNCTION(BlueprintCallable, DisplayName = "GetFolderFiles", Category = "File")
		static TArray GetFolderFiles(FString Path);
TArray UGenericArrayLibrary::FindFolder(FString Path, FString Filter, bool Files, bool Directory)
{
	TArray FilePathList;
	FilePathList.Empty();
	FFileManagerGeneric::Get().FindFilesRecursive(FilePathList, *Path, *Filter, Files, Directory);
	return FilePathList;

}
//


TArray UGenericArrayLibrary::GetFolderFiles(FString Path)
{
	TArray Files;
	FPaths::NormalizeDirectoryName(Path);
	IFileManager& FileManager = IFileManager::Get();
	FString FinalPath = Path / TEXT("*");
	FileManager.FindFiles(Files, *FinalPath, true, true);
	return Files;
}

史上最全面的UE4 文件操作,打开,读、写,增、删、改、查_第8张图片

 

你可能感兴趣的:(虚幻C++,ue4)