c# 操作TXT

数组写入到TXT:

if (!File.Exists("E:\\a.txt"))
{
  FileStream FsCreate = new FileStream("E:\\a.txt", FileMode.Create);
  FsCreate.Close();
}
FileStream fs
= new FileStream("E:\\a.txt", FileMode.Append); StreamWriter sw = new StreamWriter(fs); sw.WriteLine("strtaszfdsadfasdfadfsadf输入文本"); sw.Close(); fs.Close();

确认一下:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

把 TXT文本 读入一个 string[] 数组中:

    public string[] TxtToArray(string full_path)
    {
        if (File.Exists(full_path))
        {
          StreamReader sr = new StreamReader(full_path, Encoding.UTF8);
          string[] content_array = sr.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.None);
          sr.Close();
          return content_array;
        }
        else
        {
          return new string[1];
        }
    }

 

把 TXT文本 读入一个 string[] 数组中,并删除内容为 line_content 的元素:

    public string[] TxtDeleteLine(string full_path, string line_content)
    {
        if (File.Exists(full_path))
        {
          StreamReader sr = new StreamReader(full_path, Encoding.UTF8);
          List<string> content_list = new List<string>(sr.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.None));
          sr.Close();
          for (int i = content_list.Count - 1; i >= 0; i--)
          {
              if (content_list[i] == line_content)
              {
                content_list.RemoveAt(i);
              }
          }
          return content_list.ToArray();
        }
        else
        {
          return new string[1];
        }
    }

 

把 TXT文本 读入一个 string[] 数组中,并删除内容包含在 line_content_array 中的元素:

    public string[] TxtDeleteMultiLine(string full_path, string[] line_content_array)
    {
        if (File.Exists(full_path))
        {
          StreamReader sr = new StreamReader(full_path, Encoding.UTF8);
          string content = sr.ReadToEnd();
          sr.Close();
          List<string> content_list = new List<string>(content.Split(new string[] { "\r\n" }, StringSplitOptions.None));
          for (int i = content_list.Count - 1; i >= 0; i--)
          {
              foreach (string line in line_content_array)
              {
                if (content_list[i] == line)
                {
                    content_list.RemoveAt(i);
                    break;
                }
              }
          }
          return content_list.ToArray();
        }
        else
        {
          return new string[1];
        }
    }

 

获取两个TXT文件的差集 string[] 数组,即包含不同的行的数组:

    public string[] TxtDiff(string full_path_1, string full_path_2)
    {
        if (File.Exists(full_path_1) && File.Exists(full_path_2))
        {
            StreamReader sr_1 = new StreamReader(full_path_1, Encoding.UTF8);
            string[] content_array_1 = sr_1.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.None);
            sr_1.Close();

            StreamReader sr_2 = new StreamReader(full_path_2, Encoding.UTF8);
            string[] content_array_2 = sr_2.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.None);
            sr_2.Close();

            List<string> total_list = new List<string>();
            total_list.AddRange(content_array_1);
            total_list.AddRange(content_array_2);
            total_list.Sort();
            List<string> same_list = new List<string>();

            for (int i = 1; i < total_list.Count; i++)
            {
                if (total_list[i] == total_list[i - 1])
                {
                    same_list.Add(total_list[i]);
                    total_list.RemoveAt(i);
                    i--;
                }
            }
            foreach (string line in same_list)
            {
                for (int j = 0; j < total_list.Count; j++)
                {
                    if (total_list[j] == line)
                    {
                        total_list.RemoveAt(j);
                        j--;
                    }
                }
        }
            return total_list.ToArray();
        }
        else
        {
            return new string[1];
        }
    }

 

获取两个TXT文件的交集 string[] 数组,即包含相同的行的数组:

    public string[] TxtSame(string full_path_1, string full_path_2)
    {
        if (File.Exists(full_path_1) && File.Exists(full_path_2))
        {
        StreamReader sr_1 = new StreamReader(full_path_1, Encoding.UTF8);
        string[] content_array_1 = sr_1.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.None);
        sr_1.Close();

        StreamReader sr_2 = new StreamReader(full_path_2, Encoding.UTF8);
        string[] content_array_2 = sr_2.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.None);
        sr_2.Close();

        List<string> total_list = new List<string>();
        total_list.AddRange(content_array_1);
        total_list.AddRange(content_array_2);
        total_list.Sort();
        List<string> same_list = new List<string>();

        for (int i = 1; i < total_list.Count; i++)
        {
            if (total_list[i] == total_list[i - 1])
            {
            same_list.Add(total_list[i]);
            total_list.RemoveAt(i);
            i--;
            }
        }
        for (int j = 1; j < same_list.Count; j++)
        {
            if (same_list[j] == same_list[j - 1])
            {
            same_list.RemoveAt(j);
            j--;
            }
        }
        return same_list.ToArray();
        }
        else
        {
        return new string[1];
        }
    }

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/guanxiaokai/archive/2012/10/27/2742073.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值