"C #" reads and writes to a local TXT file

Source: Internet
Author: User

This time we want to use C # way to read and write TXT file, in the unity of the development process is also applicable, the following concrete implementation of it.

    • Create a file open, close, read, write class: myFileStream

To introduce System.IO and System.Runtime.Serialization.Formatters.Binary and, one is the file read IO class and the other is the binary class, the specific code is as follows:

Using unityengine;using system.collections;using system.runtime.serialization.formatters.binary;using System.IO;    public class myfilestream{FileStream file;    StreamReader St;    StreamWriter SW;        public bool Open (string FileName, FileAccess Mode) {bool Success = false;        String path = Pathfordocumentsfile (FileName); if (Mode = = FileAccess.Read) {if (file.exists (path)) {File = new FileStream (                Path, FileMode.Open, FileAccess.Read);                    if (file! = null) {st = new StreamReader (file);                Success = true; }}} else if (Mode = = fileaccess.write) {file = new FileStream (path, FileMode .            Create, FileAccess.Write);                if (file = null) {SW = new StreamWriter (file);            Success = true;    }} return Success; } public string ReadLine ()    {return St.    ReadLine (); } public void WriteLine (string line) {sw.    WriteLine (line); public void Close () {if (st! = null) {St.            Close ();        st = NULL; } if (sw! = null) {sw.            Close ();        SW = NULL; } if (file! = null) {file.            Close ();        FILE = null; }} public string Pathfordocumentsfile (string filename) {//temporarily only for Android and PC if (Application.platform = =            runtimeplatform.android) {string path = Application.persistentdatapath; Path = path. Substring (0, path.            LastIndexOf ('/'));        Return Path.Combine (Path, filename);            } else {string path = Application.datapath; Path = path. Substring (0, path.            LastIndexOf ('/'));        Return Path.Combine (Path, filename); }    }}

    • Create a file and field to be saved specific call class: Mylocaldata
Using unityengine;using System.collections;public class Mylocaldata:monobehaviour {public static mylocaldata Instanc        E () {if (_instance = = null) {_instance = new mylocaldata ();    } return _instance;        } public class Mylocalsavedata {public int test01;        public int test02;    public int test03;    } public Mylocalsavedata m_data = new Mylocalsavedata ();    myFileStream m_fs = new myFileStream ();    String m_filename = "LocalSaveData.txt"; public void Initload () {if (m_fs.            Open (M_filename, System.IO.FileAccess.Read)) {m_data.test01 = 0;            m_data.test02 = 0;                        m_data.test03 = 0;            int Num = 0; if (int. TryParse (M_fs.            ReadLine (), out num)) {m_data.test01 = num; } if (int. TryParse (M_fs.            ReadLine (), out num)) {m_data.test02 = num; } if (int. TrypaRSE (M_fs.            ReadLine (), out num)) {m_data.test03 = num; } m_fs.        Close (); }} public void Write () {if (m_fs. Open (M_filename, System.IO.FileAccess.Write)) {M_fs.            WriteLine (M_Data.test01.ToString ()); M_fs.            WriteLine (M_Data.test02.ToString ()); M_fs.            WriteLine (M_Data.test03.ToString ()); M_fs.        Close (); }    }}
You can mount the Mylocaldata class on a gameobject in the scene, and then you need to call for the read write code like this,read:

Mylocaldata.instance (). m_data.test01;
Write:

Mylocaldata.instance (). m_data.test01 = 1; Mylocaldata.instance (). Write ();

Ricky Yang Personal Original, All rights reserved, reproduced annotated, thank you. http://blog.csdn.net/yangyy753

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"C #" reads and writes to a local TXT file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.