- 最后登录
- 2019-12-25
- 注册时间
- 2012-8-24
- 阅读权限
- 90
- 积分
- 71088
- 纳金币
- 52304
- 精华
- 343
|
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- using System.IO;
- using System;
- public class TestLog : MonoBehaviour {
- // Use this for initialization
- void Start () {
- // 判断文件是否存在,不存在则创建,否则读取值显示到窗体
- if (!File.Exists("F:\\TestTxt.log"))
- {
- FileStream fs1 = new FileStream("F:\\TestTxt.log",FileMode.Create, FileAccess.Write);//创建写入文件
- StreamWriter sw = new StreamWriter(fs1);
- sw.WriteLine("this.textBox3.Text.Trim() + + +this.textBox4.Text");//开始写入值
- sw.Close();
- fs1.Close();
- }
- else
- {
- //FileStream fs = new FileStream("F:\\TestTxt.log", FileMode.Open, FileAccess.Write);
- FileStream fs = File.Open("F:\\TestTxt.log", FileMode.Open, FileAccess.Write);
- StreamWriter sr = new StreamWriter(fs);
- sr.WriteLine("this.textBox2.Text.Trim() + + +this.textBox4.Text");//开始写入值
- sr.WriteLine("this.textBox2.Text.Trim() + + +this.textBox4.Text");
- sr.WriteLine(16);
- sr.Close();
- fs.Close();
- //FileStream fileRead = File.Open("F:\\TestTxt.log", FileMode.Open, FileAccess.Read);
- //StreamReader ss = new StreamReader(fileRead);
- //Debug.Log(ss.ReadLine());
- //Debug.Log("----------------------------");
- }
- // 2. (FileStream fs2 = File.Open(c:\\test.txt, FileMode.Append,
- //FileAccess.Write));
- //FileMode.Append,以追加的方式打开文件c:\\test.txt,将某些内容写
- //到c:\\test.txt里。
- FileStream fileRead = new FileStream("F:\\TestTxt.log", FileMode.Open, FileAccess.Read);
- //FileStream fileRead = File.Open("F:\\TestTxt.log", FileMode.Open, FileAccess.Read);
- /*fileRead.Seek(0, SeekOrigin.Begin);*/
- //StreamReader read = File.OpenText("F:\\TestTxt.txt");
- StreamReader read = new StreamReader(fileRead);
- //read.ReadLine();
- //Debug.Log(read.ReadLine());
- //Debug.Log(read.ReadToEnd());
- //Debug.Log(read.Read());
-
- // 或者借鉴如下代码:
- // StreamWriter sw = new StreamWriter(Application.StartupPath + \\ + textBox1.Text);
- // sw.Write(textBox2.Text);
- // sw.Flush();
- // sw.Close(
- }
-
- // Update is called once per frame
- void Update () {
-
- }
- ArrayList LoadFile(string path, string name)//加载要读取的文件;
- {
- StreamReader sr = null;
- try
- {
- sr = File.OpenText(path + "//" + name);//打开文件的路径;
- }
- catch (Exception ex)
- {
- return null;
- }
- string line;
- ArrayList arrlist = new ArrayList();//定义动态数组;
- while ((line = sr.ReadLine()) != null)
- {
- arrlist.Add(line);//读取每行信息并添加到动态数组中;
- }
- sr.Close();//关闭流;
- sr.Dispose();//销毁流;
- return arrlist;//返回该动态数组;
- }
- }
复制代码 这个是不断的往一个文本中追加内容!- public void AppendLog(string message)
- {
- try
- {
- if (!File.Exists(logFile))
- {
- File.CreateText(logFile);
- }
- File.AppendAllText(logFile, "[" + DateTime.Now + "] " + message + "\r\n");
- }
- catch (Exception e)
- {
- Debug.Log(e);
- }
- }
复制代码 |
|