当前位置:   article > 正文

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

ue中的增加

一、前期操作

创建一个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"

 二、打开文件

  1. UFUNCTION(BlueprintCallable, DisplayName = "OpenFile", Category = "File")
  2. static TArray<FString> OpenFile();
  1. TArray<FString> UGenericArrayLibrary::OpenFile()
  2. {
  3. TArray<FString> FilePath; //选择文件路径
  4. FString fileType = TEXT("*.*"); //过滤文件类型
  5. FString defaultPath = FPaths::ConvertRelativePathToFull(FPaths::ProjectDir()); //文件选择窗口默认开启路径
  6. IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();
  7. bool bSuccess = DesktopPlatform->OpenFileDialog(nullptr, TEXT("打开文件"), defaultPath, TEXT(""), *fileType, EFileDialogFlags::None, FilePath);
  8. for (auto& name : FilePath)
  9. {
  10. UE_LOG(LogTemp, Warning,
  11. TEXT("%s"), *name);
  12. }
  13. if (bSuccess)
  14. {
  15. //文件选择成功,文件路径 path
  16. UE_LOG(LogTemp,Warning,TEXT("Success"));
  17. }
  18. return FilePath;
  19. }


三、读取和写入文件(字符串)

  1. //读取文件(字符串)
  2. UFUNCTION(BlueprintCallable, DisplayName = "ReadFile", Category = "File")
  3. static FString ReadFile(FString path);
  4. //写入文件(字符串)
  5. UFUNCTION(BlueprintCallable, DisplayName = "WriteFile", Category = "File")
  6. static bool WriteFile(FString saveFile,FString path);
  1. FString UGenericArrayLibrary::ReadFile(FString path)
  2. {
  3. FString resultString;
  4. FFileHelper::LoadFileToString(resultString,*path);
  5. return resultString;
  6. }
  7. ///
  8. bool UGenericArrayLibrary::WriteFile(FString saveFile,FString path)
  9. {
  10. bool success;
  11. success = FFileHelper::SaveStringToFile(saveFile,*path);
  12. return success;
  13. }

 四、读取和写入字符数组

  1. /读取文件(字符数组)
  2. UFUNCTION(BlueprintCallable, DisplayName = "ReadFileArray", Category = "File")
  3. static TArray<FString> ReadFileArray(FString path);
  4. //写入文件(字符数组)
  5. UFUNCTION(BlueprintCallable, DisplayName = "WriteFileArray", Category = "File")
  6. static bool WriteFileArray(TArray<FString> saveFile,FString path);
  1. TArray<FString> UGenericArrayLibrary::ReadFileArray(FString path)
  2. {
  3. TArray<FString> results;
  4. FFileHelper::LoadFileToStringArray(results, *path);
  5. return results;
  6. }
  7. bool UGenericArrayLibrary::WriteFileArray(TArray<FString> saveFile, FString path)
  8. {
  9. return FFileHelper::SaveStringArrayToFile(saveFile,*path);
  10. }

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

  1. //获取文件所在路径
  2. UFUNCTION(BlueprintCallable, DisplayName = "Get FilePath", Category = "File")
  3. static FString GetFilePath(FString path);
  4. //获取文件名,不带后缀
  5. UFUNCTION(BlueprintCallable, DisplayName = "GetFileName", Category = "File")
  6. static FString GetFileName(FString InPath, bool bRemovePath);
  7. //获取文件后缀
  8. UFUNCTION(BlueprintCallable, DisplayName = "GetFileExtension", Category = "File")
  9. static FString GetFileExtension(FString InPath, bool bIncludeDot);
  1. FString UGenericArrayLibrary::GetFilePath(FString path)
  2. {
  3. FString Result;
  4. Result = FPaths::GetPath(*path);
  5. return Result;
  6. }
  7. FString UGenericArrayLibrary::GetFileName(FString InPath, bool bRemovePath)
  8. {
  9. return FPaths::GetBaseFilename(*InPath,bRemovePath);
  10. }
  11. FString UGenericArrayLibrary::GetFileExtension(FString InPath, bool bIncludeDot)
  12. {
  13. return FPaths::GetExtension(*InPath,bIncludeDot);
  14. }

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

  1. //创建一个文件夹
  2. UFUNCTION(BlueprintCallable, DisplayName = "CreateFolder", Category = "File")
  3. static void CreatFolder(FString FolderName);
  4. //删除一个文件夹
  5. UFUNCTION(BlueprintCallable, DisplayName = "DeleteFolder", Category = "File")
  6. static void DeleteFolder(FString FolderName);
  1. void UGenericArrayLibrary::CreatFolder(FString FolderName)
  2. {
  3. FString Path = FPaths::ProjectDir()/ *FolderName;
  4. Path = FPaths::ConvertRelativePathToFull(*Path);
  5. FPlatformFileManager::Get().GetPlatformFile().CreateDirectoryTree(*Path);
  6. }
  7. void UGenericArrayLibrary::DeleteFolder(FString FolderName)
  8. {
  9. FString Path = FPaths::ProjectDir() / *FolderName;
  10. Path = FPaths::ConvertRelativePathToFull(*Path);
  11. FPlatformFileManager::Get().Get().GetPlatformFile().DeleteDirectoryRecursively(*Path);
  12. }

 七、移动文件夹

  1. //移动文件
  2. UFUNCTION(BlueprintCallable, Category = "MoveFileTo")
  3. static bool MoveFileTo(FString To, FString From);
  1. bool UGenericArrayLibrary::MoveFileTo(FString To, FString From)
  2. {
  3. return IFileManager::Get().Move(*To, *From);
  4. }


八、查找文件夹

  1. //查找文件目录下的所有文件
  2. UFUNCTION(BlueprintCallable, DisplayName = "FindFolder", Category = "File")
  3. static TArray<FString> FindFolder(FString Path, FString Filter, bool Files, bool Directory);
  4. //查找文件目录下所有文件无法删选查找
  5. UFUNCTION(BlueprintCallable, DisplayName = "GetFolderFiles", Category = "File")
  6. static TArray<FString> GetFolderFiles(FString Path);
  1. TArray<FString> UGenericArrayLibrary::FindFolder(FString Path, FString Filter, bool Files, bool Directory)
  2. {
  3. TArray<FString> FilePathList;
  4. FilePathList.Empty();
  5. FFileManagerGeneric::Get().FindFilesRecursive(FilePathList, *Path, *Filter, Files, Directory);
  6. return FilePathList;
  7. }
  8. //
  9. TArray<FString> UGenericArrayLibrary::GetFolderFiles(FString Path)
  10. {
  11. TArray<FString> Files;
  12. FPaths::NormalizeDirectoryName(Path);
  13. IFileManager& FileManager = IFileManager::Get();
  14. FString FinalPath = Path / TEXT("*");
  15. FileManager.FindFiles(Files, *FinalPath, true, true);
  16. return Files;
  17. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号