虚幻引擎中提供了与平台无关的文件读写与访问接口,通过调用,可以完成一些文件的读写。比如文本文件,配置文件,json文件,xml文件等。
完成文件读写,首先需要获取文件路径等相关信息。对调用这些操作,我们需要包含头文件PlatformFilemanager.h和FileHelper.h。对于json文件读写,我们需要在.build.cs文件中添加Json和JsonUtilities两个模块;对于xml文件读取,我们需要在.build.cs文件中添加XmlParser模块。引用相关模块后,在具体实现需要引入对应的头文件。我们可以创建一个继承自UBlueprintFunctionLibrary的C++类,这样我们就可以在蓝图中调用我们在C++中声明BlueprintCallable创建的函数。
#include "PlatformFilemanager.h"
#include "FileHelper.h"
#include "Json.h"
#include "JsonObject.h"
#include "JsonSerializer.h"
#include"Runtime/XmlParser/Public/XmlFile.h"
#include"Runtime/XmlParser/Public/XmlNode.h"
//文本文件读写
UFUNCTION(BlueprintCallable, Category = "Test")
static void WriteText(FString path,FString data);
UFUNCTION(BlueprintCallable, Category = "Test")
static FString ReadText(FString path);
//配置文件读写
UFUNCTION(BlueprintCallable, Category = "Test")
static void WriteConfig(FString section, FString name, FString value, FString path);
UFUNCTION(BlueprintCallable, Category = "Test")
static FString ReadConfig(FString section, FString name, FString path);
//UE4读取json文件
UFUNCTION(BlueprintCallable, Category = "Test")
static void NonSeriWriteJson();
UFUNCTION(BlueprintCallable, Category = "Test")
static void SeriWriteJson();
UFUNCTION(BlueprintCallable, Category = "Test")
static TArray ReadJson();
//UE4读取xml文件
UFUNCTION(BlueprintCallable, Category = "Test")
static void CreateXmlFile();
UFUNCTION(BlueprintCallable, Category = "Test")
static void ReadXmlFile(const FString &XmlPath);
void UFunctionLibrary::WriteText(FString path, FString data){
FString ProjectDir = FPaths::GameDir();
ProjectDir += path;
if (!ProjectDir.Contains(".txt")) {
ProjectDir += ".txt";
}
FFileHelper::SaveStringToFile(data, *ProjectDir);
}
FString UFunctionLibrary::ReadText(FString path){
FString data;
FString ProjectDir = FPaths::GameDir();
ProjectDir += path;
if (!ProjectDir.Contains(".txt")) {
ProjectDir += ".txt";
}
if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*ProjectDir)) {
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Couldn't find file"));
return FString();
}
else {
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Could find file"));
FFileHelper::LoadFileToString(data, *ProjectDir);
return data;
}
}
void UFunctionLibrary::WriteConfig(FString section, FString name, FString value, FString path){
if (!path.Contains(".ini")) {
path += ".ini";
}
GConfig->SetString(*section, *name, *value, FPaths::GameDir() / *path);
}
FString UFunctionLibrary::ReadConfig(FString section, FString name, FString path){
FString result;
if (!path.Contains(".ini")) {
path += ".ini";
}
GConfig->GetString(*section, *name, result, FPaths::GameDir() / *path);
return result;
}
void UFunctionLibrary::NonSeriWriteJson()
{
FString FilePath = FPaths::GameDir() + TEXT("MyJson.json");
FString jsonStr;
TSharedRef> jsonWriter = TJsonWriterFactory<>::Create(&jsonStr);
jsonWriter->WriteObjectStart();
jsonWriter->WriteValue(TEXT("json"), TEXT("jsonValue"));
jsonWriter->WriteObjectEnd();
jsonWriter->Close();
FFileHelper::SaveStringToFile(jsonStr, *FilePath);
}
void UFunctionLibrary::SeriWriteJson()
{
TSharedPtr RootObj = MakeShareable(new FJsonObject());
RootObj->SetStringField("root", "000");
TArray> arrValue;
TSharedPtr tmp = MakeShareable(new FJsonValueString("element"));
arrValue.Add(tmp);
RootObj->SetArrayField("array", arrValue);
FString FilePath = FPaths::GameDir() + TEXT("MySeri.json");
FString jsonStr;
TSharedRef> jsonWriter = TJsonWriterFactory::Create(&jsonStr);
FJsonSerializer::Serialize(RootObj.ToSharedRef(), jsonWriter);
FFileHelper::SaveStringToFile(jsonStr, *FilePath);
}
TArray UFunctionLibrary::ReadJson()
{
FString FilePath = FPaths::GameDir() + TEXT("MyJson.json");
if (FPaths::FileExists(FilePath))
{
FString section1;
FString section2;
FString section3;
FString section4;
TArray jsonData;
FString fileStr;
FFileHelper::LoadFileToString(fileStr, *FilePath);
TSharedPtr rootObject = MakeShareable(new FJsonObject());
TSharedRef> jsonReader = TJsonReaderFactory<>::Create(fileStr);
if (FJsonSerializer::Deserialize(jsonReader, rootObject))
{
section1 = rootObject->GetStringField("json");
section2 = rootObject->GetStringField("xml");
section3 = rootObject->GetStringField("text");
section4 = rootObject->GetStringField("config");
}
jsonData.Add(FName(*section1));
jsonData.Add(FName(*section2));
jsonData.Add(FName(*section3));
jsonData.Add(FName(*section4));
return jsonData;
}
return TArray();
}
void UFunctionLibrary::CreateXmlFile()
{
const FString XmlContent = "\n\n< ID>xml \nfile \ncccc \n \n ";
FXmlFile* WriteXml = new FXmlFile(XmlContent, EConstructMethod::ConstructFromBuffer);
if (WriteXml == nullptr)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, TEXT("fail!"));
return;
}
WriteXml->Save(FPaths::GameDir() + "xml.xml");
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Blue, TEXT("succeed!"));
}
void UFunctionLibrary::ReadXmlFile(const FString &XmlPath)
{
FXmlFile* XmlFile = new FXmlFile(XmlPath);
FXmlNode* RootNode = XmlFile->GetRootNode();
const TArray AssetNodes = RootNode->GetChildrenNodes();
for (FXmlNode* node : AssetNodes)
{
const TArray ChildNodes = node->GetChildrenNodes();
FString AssetContent = node->GetContent();
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, AssetContent);
for (FXmlNode* node : ChildNodes)
{
FString ChildContent = node->GetContent();
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Blue, ChildContent);
}
}
}
函数 | 作用 | ||||
Close() | 关闭写工具 | ||||
WriterArrayStart() | 开始一个Json数组 | ||||
WriterArrayEnd() | 结束一个Json数组 | ||||
WriterNull(FString) | 为一个键写入空值 | ||||
WriterObjectStart() | 开始一个Json对象 | ||||
WriterObjectEnd() | 结束一个Json对象 | ||||
WriterValue(FString,FString/int32/float/bool) | 向Json文件写入键值对 |
FJsonSerializer::Deserialize(TSharePtr>,TSahrePtr)
将输入流载体的Json数据反序列化到FJsonObject对象中,最后使用FJsonObject对象中的GetArrayField(FString)、GetBoolFiled(FString)、GetNumberField(FString)、GetStringField(FString)、GetObjectField(FString)、GetField(FString)等方法从Json对象中读取指定键的值。