纳金网

标题: 读写本地文件的代码 [打印本页]

作者: may    时间: 2015-6-28 23:39
标题: 读写本地文件的代码
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.IO;
  5. using System;

  6. public class TestLog : MonoBehaviour {

  7.         // Use this for initialization
  8.         void Start () {
  9.             // 判断文件是否存在,不存在则创建,否则读取值显示到窗体
  10.         if (!File.Exists("F:\\TestTxt.log"))
  11.         {
  12.             FileStream fs1 = new FileStream("F:\\TestTxt.log",FileMode.Create, FileAccess.Write);//创建写入文件
  13.             StreamWriter sw = new StreamWriter(fs1);
  14.             sw.WriteLine("this.textBox3.Text.Trim() + + +this.textBox4.Text");//开始写入值
  15.             sw.Close();
  16.             fs1.Close();
  17.         }
  18.         else
  19.         {
  20.             //FileStream fs = new FileStream("F:\\TestTxt.log", FileMode.Open, FileAccess.Write);
  21.             FileStream fs = File.Open("F:\\TestTxt.log", FileMode.Open, FileAccess.Write);
  22.             StreamWriter sr = new StreamWriter(fs);
  23.             sr.WriteLine("this.textBox2.Text.Trim() + + +this.textBox4.Text");//开始写入值
  24.             sr.WriteLine("this.textBox2.Text.Trim() + + +this.textBox4.Text");
  25.             sr.WriteLine(16);
  26.             sr.Close();
  27.             fs.Close();
  28.             //FileStream fileRead = File.Open("F:\\TestTxt.log", FileMode.Open, FileAccess.Read);
  29.             //StreamReader ss = new StreamReader(fileRead);
  30.             //Debug.Log(ss.ReadLine());
  31.             //Debug.Log("----------------------------");
  32.         }


  33. //        2. (FileStream fs2 = File.Open(c:\\test.txt, FileMode.Append,
  34. //FileAccess.Write));
  35. //FileMode.Append,以追加的方式打开文件c:\\test.txt,将某些内容写
  36. //到c:\\test.txt里。


  37.         FileStream fileRead = new FileStream("F:\\TestTxt.log", FileMode.Open, FileAccess.Read);
  38.         //FileStream fileRead = File.Open("F:\\TestTxt.log", FileMode.Open, FileAccess.Read);
  39.         /*fileRead.Seek(0, SeekOrigin.Begin);*/
  40.         //StreamReader read = File.OpenText("F:\\TestTxt.txt");
  41.         StreamReader read = new StreamReader(fileRead);
  42.         //read.ReadLine();
  43.         //Debug.Log(read.ReadLine());
  44.         //Debug.Log(read.ReadToEnd());
  45.         //Debug.Log(read.Read());
  46.         

  47. //         或者借鉴如下代码:
  48. //         StreamWriter sw = new StreamWriter(Application.StartupPath + \\ + textBox1.Text);
  49. //         sw.Write(textBox2.Text);
  50. //         sw.Flush();
  51. //         sw.Close(       
  52.     }
  53.        
  54.         // Update is called once per frame
  55.         void Update () {
  56.        
  57.         }

  58.     ArrayList LoadFile(string path, string name)//加载要读取的文件;
  59.     {
  60.         StreamReader sr = null;
  61.         try
  62.         {
  63.             sr = File.OpenText(path + "//" + name);//打开文件的路径;
  64.         }
  65.         catch (Exception ex)
  66.         {
  67.             return null;
  68.         }
  69.         string line;
  70.         ArrayList arrlist = new ArrayList();//定义动态数组;
  71.         while ((line = sr.ReadLine()) != null)
  72.         {
  73.             arrlist.Add(line);//读取每行信息并添加到动态数组中;
  74.         }
  75.         sr.Close();//关闭流;
  76.         sr.Dispose();//销毁流;
  77.         return arrlist;//返回该动态数组;
  78.     }


  79. }
复制代码
这个是不断的往一个文本中追加内容!
  1. public void AppendLog(string message)
  2.     {
  3.         try
  4.         {
  5.             if (!File.Exists(logFile))
  6.             {
  7.                 File.CreateText(logFile);
  8.             }

  9.             File.AppendAllText(logFile, "[" + DateTime.Now + "] " + message + "\r\n");
  10.         }
  11.         catch (Exception e)
  12.         {
  13.             Debug.Log(e);
  14.         }
  15.     }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5