查看: 637|回复: 0
打印 上一主题 下一主题

[其他] 读写本地文件的代码

[复制链接]
may    

8830

主题

80

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
52304
精华
343

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2015-6-28 23:39:22 |只看该作者 |倒序浏览
  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.     }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-4 21:52 , Processed in 0.084335 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部