CoreMisc.h
读取全部内容,存到 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);
}
读读取全部内容,存到 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);
}
}
字符串写入文件,需要手动嘉 \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);
}
字符串数组写入文件,自动换行
如果保存中文乱码,点击高级保存为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);
手机扫一扫
移动阅读更方便
你可能感兴趣的文章