TxT写入 读出

来源:互联网 发布:淘宝抢鞋软件 编辑:程序博客网 时间:2024/06/05 19:08
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace WindowsFormsApplication6{    class TxTWriteAndRead    {        #region 读入到text中        /// <summary>        /// 读入到text中        /// </summary>        /// <param name="FullFileName"></param>        /// <param name="TextAll"></param>        /// <returns></returns>        public static bool TxtExport(string FullFileName, List<string[]> strContent)        {            if (!CreatFile(FullFileName))            {                return false;            }            StringBuilder sb = new StringBuilder();            foreach (string[] item in strContent)            {  if(item.length>=2)  
            {              
sb.Append(item[0] + ":" + item[1] + "\r\n");
    }            }            StreamWriter sw = new StreamWriter(FullFileName, true, Encoding.Default);   //该编码类型不会改变已有文件的编码类型            sw.WriteLine(sb.ToString());            sw.Close();            return true;        }        #endregion        #region 创建文件(文件存在则跳过)        /// <summary>        /// 创建文件(文件存在则跳过)        /// </summary>        /// <param name="FullFileName">带路径的文件名</param>        /// <returns></returns>        public static bool CreatFile(string FullFileName)        {            if (File.Exists(FullFileName))            {                return true;            }            else            {                try                {                    FileStream fs = new FileStream(FullFileName, FileMode.CreateNew);                    fs.Close();                    return true;                }                catch                 {                    return false;                }            }        }        #endregion        public static List<string[]> ReadToList(string FullFileName)        {            List<string[]> li = new List<string[]>();            if (CreatFile(FullFileName))            {                StreamReader sr = new StreamReader(FullFileName, Encoding.Default);              while (true)                {                    string strR = sr.ReadLine();                    if (strR == null)                    {                        break;                    }                  li.Add(strR.Split(':'));                }              sr.Close();            }            return li;        }    }}

如果 有好的改良方案,敬请指正。。
原创粉丝点击