TxT写入 读出

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;
        }
    }
}

如果 有好的改良方案,敬请指正。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值