Unity3d read and write TXT files

Source: Internet
Author: User

Using unityengine;
Using system. IO;
Using system;
Using system. collections;
Public class cgraettext: monobehaviour
{
Private string m_sfilename = "filename.txt"; // file name
Private string m_spath = application. persistentdatapath; // path
Private arraylist m_aarray; // content of each line in the text
/*
* Spath: directory for File Creation
* Sname: File Name
* Ndate: Data
*/
Void fncreatefile (string Spath, string sname, int ndate)
{
Streamwriter t_sstreamwriter; // file stream information
Fileinfo t_ffileinfo = new fileinfo (Spath + "//" + sname );
If (! T_ffileinfo.exists)
{
T_sstreamwriter = t_ffileinfo.createtext (); // if this file does not exist, create
}
Else
{
T_sstreamwriter = t_ffileinfo.appendtext (); // open the file if it exists.
}
T_sstreamwriter.writeline (ndate); // write information in the form of rows
T_sstreamwriter.close (); // close the stream.
T_sstreamwriter.dispose (); // destroy the stream
}
/*
* Path: the path to read the file.
* Name: name of the file to be read
*/
Arraylist fnloadfile (string Spath, string sname)
{
Streamreader t_sstreamreader = NULL; // read data in the form of a stream
// Try
//{
T_sstreamreader = file. opentext (Spath + "//" + sname );
//}
// Catch (exception ex)
//{
// Return NULL;
//}
String t_sline; // the content of each line
Arraylist t_javasraylist = new arraylist (); // container
While (t_sline = t_sstreamreader.readline ())! = NULL)
{
T_javasraylist.add (t_sline); // Save the content of each row to the array linked list container.
}
T_sstreamreader.close (); // close the stream

T_sstreamreader.dispose (); // destroy the stream

Return t_javasraylist; // return the array linked list container
}
/*
* Spath: Path of the file to be deleted
* Sname: name of the deleted object
*/
Void fndeletefile (string Spath, string sname)
{
File. Delete (Spath + "//" + sname );
}
}

 

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.