TXT

来源:互联网 发布:软件开发测试 编辑:程序博客网 时间:2024/04/24 21:31
using UnityEngine;using System.Collections;using System.IO ; //I/O操作的命名空间using UnityEditor;//unity编辑器的引用空间public class Data_TXT : MonoBehaviour {private ArrayList _AtrrayList;void Start () {   //打印路径print (Application.dataPath);}//end_Startvoid Update () {if (Input.GetKeyDown(KeyCode.C)) { //创建文件并写入数据    路径              文件名称         要写入的信息CreateFile (Application.dataPath, "SJU161101.txt","本班都是天才" );CreateFile (Application.dataPath, "SJU161101.txt","本班都是帅哥");//刷新Project视图//AssetDatabase.Refresh();}if (Input.GetKeyDown(KeyCode.R)) {//读取文件_AtrrayList = LoadFile (Application.dataPath,"SJU161101.txt");foreach (string  item in _AtrrayList) {print (item);}}if (Input.GetKeyDown(KeyCode.D)) {DeleteFile (Application.dataPath,"SJU161101.txt");//预编译命令#if UNITY_EDITOR_OSX    //刷新project视图AssetDatabase.Refresh();#elif UNITY_ANDROIOprint(111);#endif}}//end_Update/// <summary>/// 创建文件并写入数据/// </summary>/// <param name="path">路径</param>/// <param name="name">文件名称.</param>/// <param name="info">要写入的信息</param>void CreateFile(string path,string name, string info ){StreamWriter sw; //写入流FileInfo fi = new FileInfo (path + "//" + name); if (!fi.Exists) {//如果文件不存在,创建路径sw = fi.CreateText();} else { //如果存在,打开sw = fi.AppendText();}//现在fi一定存在//以行的形式写入信息sw.WriteLine(info);//关闭流sw.Close();//销毁流sw.Dispose ();}/// <summary>/// 读取文件/// </summary>/// <returns>文件的信息</returns>/// <param name="path">文件路径.</param>/// <param name="name">文件名称.</param>ArrayList LoadFile(string path, string name){StreamReader  sr; //读取流try {//尝试、试图sr = File.OpenText(path + "//" + name);//使用FileInfo写的话//FileInfo fi = new FileInfo (path + "//" + name);//sr = fi.OpenText ();} catch (System.Exception ex) {//捕获 异常(运行时出现的错误)return null; //抛出捕获的异常}string strLine;// 行的信息ArrayList arrayList = new ArrayList ();//sr读取到不为空,则一直循环while ((strLine = sr.ReadLine()) != null) {arrayList.Add (strLine);}//关闭流sr.Close ();//销毁流sr.Dispose ();return arrayList;}void DeleteFile(string path, string name){   //静态方法File.Delete (path +"//" + name);//普通方法//FileInfo fi = new FileInfo (path + "//" + name);//fi.Delete ();}}


原创粉丝点击