【Unity3D读取数据】(一)Txt文档操作(创建、读取、写入、修改)
  4UZE4BBN2hyC 2023年11月02日 47 0

大家好,我是佛系工程师☆恬静的小魔龙☆,不定时更新Unity开发技巧,觉得有用记得一键三连哦。

一、前言

在程序开发中,经常会遇到要从外部文件中读取数据的情况,文件类型也比较多。

于是,小魔龙就将常见的文件类型:Txt、Json、Xml、Excel、Csv,读取、修改、保存等常见操作代码总结下来。

一方面可以自己复习,另一方面也希望可以帮助更多的人吧。

这是本系列文章第一篇:

【Unity3D读取数据】(一)Txt文档操作(创建、读取、写入、修改)

【Unity3D读取数据】(二)Json文件操作(创建、读取、解析、修改)

【Unity3D读取数据】(三)Xml文件操作(创建、读取、写入、修改)

【Unity3D读取数据】(四)Excel文件操作(创建、读取、写入、修改)

【Unity3D读取数据】(五)Csv文件操作(创建、读取、写入、修改)

二、唠叨两句

读取txt其实是很简单的,用File类、FileStream类、 SteamReader类、 StreamWriter类都可以读取文件。

主要就是分为两种形式,一种是文件读取的形式,一种就是流数据的形式。

读取txt的类很多,读取的形式也很多,有的整篇读取,有的一行一行读取。

读取是第一步,保存数据是第二步,读取的方式也影响了保存数据的方式,如果是一行一行读取,那么就可以一行一行解析,然后保存到数据集合中,如果是整篇读取,就需要对整篇就行每行解析,然后通过分隔符就行分割保存。

三、读取txt文档

我们新建一个名字叫做TextRead.txt的文档,键入内容后保存。

【Unity3D读取数据】(一)Txt文档操作(创建、读取、写入、修改)_读取文档

要注意的一点是,文档格式设置为UTF-8,不然中文可能显示不太正确。

3-1、通过TextAsset类读取文档

先来一个最简单的读取方法:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
public TextAsset textTxt;

void Start()
{
Debug.Log(textTxt.text);
}
}

把文档拖到对应的卡槽中:

【Unity3D读取数据】(一)Txt文档操作(创建、读取、写入、修改)_unity_02

运行结果:

【Unity3D读取数据】(一)Txt文档操作(创建、读取、写入、修改)_txt_03

3-2、通过File类读取文件

通过File类的ReadAllText()把文档所有内容读取下来:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
void Start()
{
string textTxt = File.ReadAllText(Application.streamingAssetsPath + "/TextRead.txt");
Debug.Log(textTxt);
}
}

也可以使用File类的ReadAllLines()函数,将这个文档按照一行一行进行全部读取:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
void Start()
{
string[] textTxt = File.ReadAllLines(Application.streamingAssetsPath + "/TextRead.txt");
for (int i = 0; i < textTxt.Length; i++)
{
Debug.Log(textTxt[i]);
}
}
}

上面两个函数都各自有一个重载函数:

public static string[] ReadAllLines(string path);
public static string[] ReadAllLines(string path, Encoding encoding);
public static string ReadAllText(string path, Encoding encoding);
public static string ReadAllText(string path);

可以以设定的文档格式打开文档。

3-3、以文件流的形式读取文档

通过IO命名空间下的FileStream类进行读取文档数据:

这是第一种方式,通过FileStream类的实例化方法去加载文件:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
void Start()
{
string path = Application.streamingAssetsPath + "/TextRead.txt";
//文件流形式读取文档
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
string str= Encoding.UTF8.GetString(bytes);
Debug.Log(str);
}
}
}

还可以通过File类的OpenRead()函数加载文档:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
void Start()
{
string path = Application.streamingAssetsPath + "/TextRead.txt";
//文件流形式读取文档
using (FileStream fs = File.OpenRead(Application.streamingAssetsPath + "/TextRead.txt"))
{
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
string str = Encoding.UTF8.GetString(bytes);
Debug.Log(str);
}
}
}

仔细看,两者还是有差别的。。。

3-4、以流形式读取文档

通过IO命名空间下的StreamReader类以流形式读取文档:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
void Start()
{
string path = Application.streamingAssetsPath + "/TextRead.txt";
//流形式读取文档
using (StreamReader sr = new StreamReader(path))
{
string content = sr.ReadToEnd();
sr.Close();
sr.Dispose();
Debug.Log(content);
}
}
}

还可以使用File类的OpenText()函数以流形式读取文档:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
void Start()
{
string path = Application.streamingAssetsPath + "/TextRead.txt";
//流形式读取文档
using (StreamReader sr = File.OpenText(path))
{
string content = sr.ReadToEnd();
sr.Close();
sr.Dispose();
Debug.Log(content);
}
}
}

注意看,还是有区别的。。。

四、修改数据保存文档

4-1、通过File类写入数据

还记得怎么读取数据吗?

File.ReadAllText()函数及ReadAllLines()函数

那么写入数据就使用:

File.WriteAllText()函数及ReadWriteLines()函数

File.WriteAllText()

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
void Start()
{
string path = Application.streamingAssetsPath + "/TextRead.txt";
File.WriteAllText(path, "测试数据");
}
}

ReadWriteLines()

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
void Start()
{
string path = Application.streamingAssetsPath + "/TextRead.txt";
string[] content = { "测试数据1", "测试数据2", "测试数据3" };
File.WriteAllLines(path, content);
}
}

WriteAllText()是将整个文本保存到文档中。

ReadWriteLines()函数是将一个string数组保存到文档中。

4-2、通过文件流的形式写入数据

有读就有写:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
void Start()
{
string path = Application.streamingAssetsPath + "/TextRead.txt";
string content = "测试文档";
//文件流形式读取文档
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Write))
{
byte[] bytes = Encoding.UTF8.GetBytes(content);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
}
}
}

4-3、通过流形式写入数据

有StreamReader类就有StreamWriter类,哎,就是这么方便:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
void Start()
{
string path = Application.streamingAssetsPath + "/TextRead.txt";
string content = "测试文档";
using (StreamWriter sr = new StreamWriter(path))
{
sr.WriteLine(content);
sr.Close();
sr.Dispose();
Debug.Log(content);
}
}
}

五、后言

本篇文章讲解了使用各种方法读取文档的方式,有通过File类去读取文档,有通过文件流形式去读取文档,有通过流形式去读取文档。

这些读取的操作都需要引入IO命名空间。

基本所有的文档的读取方式都离开上面的几种方式,接下来分享的读取json文档、xml文档、Excel文档,基本都是用上面的几种读取方式。

如果到后面读取不太懂的,可以在这篇文章再复习一下。



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

4UZE4BBN2hyC