This is the 27th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Recommended reading

  • CSDN home page
  • GitHub open source address
  • Unity3D plugin sharing
  • Jane’s address book
  • My personal blog
  • QQ group: 1040082875

Hello everyone, I am a Buddhist engineer ☆ quiet small magic dragon ☆, update Unity development skills from time to time, think useful remember one key three link oh.

One, foreword

In the program development, we often encounter the situation of reading data from external files, and there are many file types.

Therefore, the small demon dragon will be common file types: Txt, Json, Xml, Excel, Csv, read, modify, save and other common operation code summary down.

On the one hand, I can review by myself. On the other hand, I hope I can help more people.

Two, nag two words

Read TXT is actually very simple, with the File class, FileStream class, SteamReader class, StreamWriter class can read the File.

It is mainly divided into two forms, one is the form of file reading, one is the form of stream data.

TXT read a lot of classes, read a lot of forms, some whole read, some line by line read.

Reading is the first step, save the data is the second, reading way also influence the way save the data, if it is read one line, you can line, and then save to the data collection, if it is to read the whole paper, you need to the whole line, each row and then split by the delimiter.

Three, read TXT documents

Let’s create a new document called Textre.txt, type it and save.One thing to note is that the document format is set to UTF-8, otherwise Chinese may not be displayed correctly.

3-1. Read the document from the TextAsset class

Let’s start with the simplest way to read:

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

public class Demo5 : MonoBehaviour
{
    public TextAsset textTxt;

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

Drag the document into the corresponding card slot:Running results:

3-2. Read the File from the File class

ReadAllText() reads the entire contents of the document from the File class:

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); }}Copy the code

You can also use the ReadAllLines() function of the File class to read the entire document line by line:

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]); }}}Copy the code

Each of the above functions has an overloaded function:

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);
Copy the code

You can open a document in a specified document format.

3-3. Read the document as a file stream

Read the document data using the FileStream class in the IO namespace:

This is the first way to load files using the FileStream class’s instantiation method:

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";
        // Read the document as a file stream
        using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            byte[] bytes = new byte[fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            stringstr= Encoding.UTF8.GetString(bytes); Debug.Log(str); }}}Copy the code

Documents can also be loaded through the OpenRead() function of the File class:

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";
        // Read the document as a file stream
        using (FileStream fs = File.OpenRead(Application.streamingAssetsPath + "/TextRead.txt"))
        {
            byte[] bytes = new byte[fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            stringstr = Encoding.UTF8.GetString(bytes); Debug.Log(str); }}}Copy the code

Look closely, there’s a difference…

3-4. Read the document as a stream

Read documents as streams through the StreamReader class in the IO namespace:

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";
        // Read the document in stream form
        using (StreamReader sr = new StreamReader(path))
        {
            stringcontent = sr.ReadToEnd(); sr.Close(); sr.Dispose(); Debug.Log(content); }}}Copy the code

You can also read the document as a stream using the OpenText() function of the File class:

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";
        // Read the document in stream form
        using (StreamReader sr = File.OpenText(path))
        {
            stringcontent = sr.ReadToEnd(); sr.Close(); sr.Dispose(); Debug.Log(content); }}}Copy the code

Look, there’s a difference…

4. Modify the data preservation document

4-1. Write data to the File class

Remember how to read data? ReadAllText() and readwriteline () functions file.writealltext () and readwriteline () functions 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, "Test data"); }}Copy the code

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 = { "Test Data 1"."Test Data 2"."Test Data 3"}; File.WriteAllLines(path, content); }}Copy the code

WriteAllText() is to save the entire text to a document. The ReadWriteLines() function saves an array of strings to a document.

4-2. Write data in the form of file flow

While you read, you write:

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 = "Test document";
        // Read the document as a file stream
        using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Write))
        {
            byte[] bytes = Encoding.UTF8.GetBytes(content);
            fs.Write(bytes, 0, bytes.Length); fs.Close(); }}}Copy the code

4-3. Write data in stream form

StreamReader: StreamWriter: StreamReader: StreamWriter: 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 = "Test document";
        using (StreamWriter sr = newStreamWriter(path)) { sr.WriteLine(content); sr.Close(); sr.Dispose(); Debug.Log(content); }}}Copy the code

Five, after the speech

This article explains how to read documents using various methods, including the File class, File stream, and stream.

These read operations need to import the IO namespace.

Basically, all the reading methods of documents are different from the above methods, and the following reading methods of JSON documents, XML documents and Excel documents are basically the above methods.

If you don’t understand it, you can review it again in this article.