【UE4 C++】讀寫Text文件 FFileHelper


CoreMisc.h

讀取

FFileHelper::LoadFileToString

  • 讀取全部內容,存到 FString

    FString TextPath = FPaths::ProjectDir() + TEXT("DataDrive/Dialogue.txt");
    TextPath = FPaths::ConvertRelativePathToFull(TextPath);
    if (FPlatformFileManager::Get().GetPlatformFile().FileExists(*TextPath))
    {
    	FString TextData;
    	FFileHelper::LoadFileToString(TextData, *TextPath);
    	UE_LOG(LogTemp, Warning,TEXT("%s"), *TextData);
    }
    

FFileHelper::LoadFileToStringArray

  • 讀讀取全部內容,存到 TArray 中,每個元素代表一行

    FString TextPath = FPaths::ProjectDir() + TEXT("DataDrive/Dialogue.txt");
    	TextPath = FPaths::ConvertRelativePathToFull(TextPath);
    	if (FPlatformFileManager::Get().GetPlatformFile().FileExists(*TextPath))
    	{
    		TArray<FString> TextData;
    		FFileHelper::LoadFileToStringArray(TextData, *TextPath);
    		int32 line = 0;
    		for (FString lineContent : TextData)
    		{
    			UE_LOG(LogTemp, Warning, TEXT("[line %d] %s"), ++line, *lineContent);
    		}	
    	}
    

    image

寫入

SaveStringToFile

  • 字符串寫入文件,需要手動嘉 \n 換行

  • 注意字符編碼

  • 注意WriteFlags,參考下面 EFileWrite 枚舉

  • Syntax

    enum EFileWrite
     {
         FILEWRITE_NoFail            = 0x01,
         FILEWRITE_NoReplaceExisting = 0x02,
         FILEWRITE_EvenIfReadOnly    = 0x04,
         FILEWRITE_Append            = 0x08,
         FILEWRITE_AllowRead         = 0x10
     };
    
    bool FFileHelper::SaveStringToFile( FStringView String, const TCHAR* Filename,  EEncodingOptions EncodingOptions, IFileManager* FileManager /*= &IFileManager::Get()*/, uint32 WriteFlags )
    
  • 代碼實現

    FString TextContent = TEXT("明知無法戰勝,明知毫無意義,但還是持續挑戰着的姿態,那正是我的過錯。----英靈衛宮 \n");
    FFileHelper::SaveStringToFile(TextContent, *TextPath, FFileHelper::EEncodingOptions::ForceUTF8, &IFileManager::Get(), EFileWrite::FILEWRITE_Append);
    
    TextContent = TEXT("束縛於肉體中的你們,永遠無法滿足 ----吉爾伽美什 \n");
    FFileHelper::SaveStringToFile(TextContent, *TextPath, FFileHelper::EEncodingOptions::ForceUTF8, &IFileManager::Get(), EFileWrite::FILEWRITE_Append);
    
  • 支持導出CSV

    UserInfoDataTable = LoadObject<UDataTable>(this, TEXT("DataTable'/Game/CPPFunction/DataDrive/DT_UserInfo.DT_UserInfo'"));
    if (UserInfoDataTable)
    {
    	FString CSVString = UserInfoDataTable->GetTableAsCSV();
    	FString CSVPath = FPaths::ProjectDir() + TEXT("DataDrive/UserInfo2.csv");
    	FFileHelper::SaveStringToFile(CSVString, *CSVPath, FFileHelper::EEncodingOptions::ForceUTF8);
    }
    

SaveStringArrayToFile

  • 字符串數組寫入文件,自動換行

  • 如果保存中文亂碼,點擊高級保存為UTF8格式

  • Syntax

    bool FFileHelper::SaveStringArrayToFile( const TArray<FString>& Lines, const TCHAR* Filename, EEncodingOptions EncodingOptions, IFileManager* FileManager, uint32 WriteFlags )
    
  • 代碼實現

    FString TextPath = FPaths::ProjectDir() + TEXT("DataDrive/FateNight.txt");
    TextPath = FPaths::ConvertRelativePathToFull(TextPath);
    TArray<FString> TextLines;
    TextLines.Add(TEXT("鋼鐵為身,而火焰為血。"));
    TextLines.Add(TEXT("手制之劍已達千余,縱橫無數戰場而不敗。"));
    TextLines.Add(TEXT("不知生。"));
    TextLines.Add(TEXT("亦不知死。"));
    TextLines.Add(TEXT("未曾一次敗退。"));
    TextLines.Add(TEXT("常獨自一人立於劍丘之巔獨醉於勝利中。"));
    TextLines.Add(TEXT("然而 留下的只有虛無。"));
    TextLines.Add(TEXT("故此 此生已無意義。"));
    TextLines.Add(TEXT("則此軀 注定為劍而生。"));
    TextLines.Add(TEXT("故我祈求——無限之劍制(Unlimited Blade Works)"));	
    FFileHelper::SaveStringArrayToFile(TextLines, *TextPath,FFileHelper::EEncodingOptions::ForceUTF8);
    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM