.Net C# Txt文本文件操作

写入

StreamWriter 类

StreamWriter 类 MSDN介绍

StreamWriter 构造函数 (String)

新实例初始化 StreamWriter 类为指定的文件使用默认的编码和缓冲区大小。

File类 MSDN介绍

File.AppendText 方法 (String)
创建一个 StreamWriter,它将 UTF-8 编码文本追加到现有文件或新文件(如果指定文件不存在)。

//AppendText 原型
public static StreamWriter AppendText(
    string path
)
using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "test.txt";
            string textToAdd = "Example text in file";
            using (StreamWriter writer = new StreamWriter(fileName))
            {
                //1,写入文本
                writer.Write(textToAdd);
            }
            //2,追加文本
            StreamWriter sw = File.AppendText(fileName);
            sw.Write(textToAdd);//不换行
            sw.WriteLine(textToAdd);//自动换行
            sw.Close();
        }
    }
}

读取

static void Main(string[] args)
        {
            System.IO.StreamReader sr = System.IO.File.OpenText(@"E:/TIMEOUT.TXT");
            //string[] xx = sr.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.None);//StringSplitOptions.None 分割后返回原值。sr.ReadToEnd()后再次sr.ReadToEnd()内容将为空。
            string[] yy = sr.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Where(t=>t.EndsWith("strat") == true).ToArray(); //StringSplitOptions.RemoveEmptyEntries 分割后内容为空则不返回。.EndsWith("strat")取以“strat”结尾的结果
            //将结果写入文本
            StreamWriter sw = File.AppendText(@"E:/timeoutE.TXT");
            sw.Write(string.Join("\r\n",yy));
            sw.Close();
        }

测试源文件

10/21/2017 11:27:53 AM :
10/21/2017 3:27:53 AMECN17-0000035182 CALL EFGP strat

10/21/2017 11:28:30 AM :
10/21/2017 3:28:30 AMPAP17-0000026659 CALL EFGP strat

10/21/2017 11:30:26 AM :
10/21/2017 3:30:26 AMMR17-0000001568 CALL EFGP strat

10/21/2017 11:32:11 AM :
10/21/2017 3:32:11 AMMR17-0000001568 CALL EFGP strat

10/21/2017 11:33:40 AM :
10/21/2017 3:33:40 AMMR17-0000001568 CALL EFGP strat

10/21/2017 11:36:00 AM :
10/21/2017 3:36:00 AMMR17-0000001568 CALL EFGP strat

10/21/2017 11:36:15 AM :
10/21/2017 3:36:15 AMPTA17-0000037906 CALL EFGP strat

10/21/2017 11:36:43 AM :
10/21/2017 3:36:43 AMMR17-0000001568 CALL EFGP strat

10/21/2017 11:38:02 AM :
10/21/2017 3:38:02 AMPTA17-0000037906 CALL EFGP strat

10/21/2017 11:38:54 AM :
10/21/2017 3:38:54 AMMR17-0000001569 CALL EFGP strat

10/21/2017 11:40:35 AM :
10/21/2017 3:40:35 AMPTA17-0000037910 CALL EFGP strat

10/21/2017 11:40:35 AM :
10/21/2017 3:40:35 AMPTA17-0000037909 CALL EFGP strat

10/21/2017 11:41:05 AM :
10/21/2017 3:41:05 AMPTA17-0000037904 CALL EFGP strat

10/21/2017 11:42:08 AM :
10/21/2017 3:42:08 AMMR17-0000001569 CALL EFGP strat

10/21/2017 11:42:44 AM :
10/21/2017 3:42:44 AMMR17-0000001568 CALL EFGP strat

10/21/2017 11:43:11 AM :
10/21/2017 3:43:11 AMMR17-0000001568 CALL EFGP strat

10/21/2017 11:43:12 AM :
10/21/2017 3:43:12 AMPTA17-0000037912 CALL EFGP strat

10/21/2017 11:43:23 AM :
10/21/2017 3:43:23 AMPTA17-0000037904 CALL EFGP strat

10/21/2017 11:43:51 AM :
10/21/2017 3:43:51 AMAPP17-0000017880 CALL EFGP strat

10/21/2017 11:46:03 AM :
10/21/2017 3:46:03 AMAPP17-0000017880 CALL EFGP strat

10/21/2017 11:46:25 AM :
10/21/2017 3:46:25 AMPTA17-0000037906 CALL EFGP strat

10/21/2017 11:46:38 AM :
10/21/2017 3:46:38 AMPTA17-0000037910 CALL EFGP strat

10/21/2017 11:46:44 AM :
10/21/2017 3:46:44 AMPTA17-0000037913 CALL EFGP strat

10/21/2017 11:47:06 AM :
10/21/2017 3:47:06 AMAPP17-0000017880 CALL EFGP strat

10/21/2017 11:47:07 AM :
10/21/2017 3:47:07 AMPTA17-0000037906 CALL EFGP strat

10/21/2017 11:48:25 AM :
10/21/2017 3:48:25 AMPTA17-0000037910 CALL EFGP strat

10/21/2017 11:48:26 AM :
10/21/2017 3:48:26 AMECN17-0000035187 CALL EFGP strat

10/21/2017 11:48:32 AM :
10/21/2017 3:48:32 AMPTA17-0000037913 CALL EFGP strat

10/21/2017 11:50:14 AM :
10/21/2017 3:50:14 AMAPP17-0000017880 CALL EFGP strat

10/21/2017 11:50:34 AM :
10/21/2017 3:50:34 AMPTA17-0000037904 CALL EFGP strat

10/21/2017 11:50:41 AM :
10/21/2017 3:50:41 AMPTA17-0000037909 CALL EFGP strat

10/21/2017 11:50:42 AM :
10/21/2017 3:50:42 AMPTA17-0000037913 CALL EFGP strat

10/21/2017 11:51:38 AM :
10/21/2017 3:51:38 AMSAP17-0000017299 CALL EFGP strat

10/21/2017 11:52:32 AM :
10/21/2017 3:52:32 AMAPP17-0000017880 CALL EFGP strat

10/21/2017 11:52:35 AM :
10/21/2017 3:52:35 AMPTA17-0000037909 CALL EFGP strat

10/21/2017 11:52:41 AM :
10/21/2017 3:52:41 AMPTA17-0000037904 CALL EFGP strat

10/21/2017 11:53:05 AM :
10/21/2017 3:53:05 AMPTA17-0000037909 CALL EFGP strat

10/21/2017 11:55:28 AM :
10/21/2017 3:55:28 AMAPP17-0000017880 CALL EFGP strat
106
10/21/2017 11:35:52 AM :
10/21/2017 3:35:52 AMECN17-0000035167 CALL EFGP strat

10/21/2017 11:45:49 AM :
10/21/2017 3:45:49 AMPTA17-0000037911 CALL EFGP strat

10/21/2017 11:46:38 AM :
10/21/2017 3:46:38 AMPTA17-0000037914 CALL EFGP strat

10/21/2017 11:47:21 AM :
10/21/2017 3:47:21 AMECN17-0000035148 CALL EFGP strat

10/21/2017 11:48:27 AM :
10/21/2017 3:48:27 AMPTA17-0000037914 CALL EFGP strat

10/21/2017 11:49:03 AM :
10/21/2017 3:49:03 AMPTA17-0000037914 CALL EFGP strat

10/21/2017 11:49:34 AM :
10/21/2017 3:49:34 AMECN17-0000035148 CALL EFGP strat

10/21/2017 11:51:41 AM :
10/21/2017 3:51:41 AMPTA17-0000037918 CALL EFGP strat

结果

10/21/2017 3:27:53 AMECN17-0000035182 CALL EFGP strat
10/21/2017 3:28:30 AMPAP17-0000026659 CALL EFGP strat
10/21/2017 3:30:26 AMMR17-0000001568 CALL EFGP strat
10/21/2017 3:32:11 AMMR17-0000001568 CALL EFGP strat
10/21/2017 3:33:40 AMMR17-0000001568 CALL EFGP strat
10/21/2017 3:36:00 AMMR17-0000001568 CALL EFGP strat
10/21/2017 3:36:15 AMPTA17-0000037906 CALL EFGP strat
10/21/2017 3:36:43 AMMR17-0000001568 CALL EFGP strat
10/21/2017 3:38:02 AMPTA17-0000037906 CALL EFGP strat
10/21/2017 3:38:54 AMMR17-0000001569 CALL EFGP strat
10/21/2017 3:40:35 AMPTA17-0000037910 CALL EFGP strat
10/21/2017 3:40:35 AMPTA17-0000037909 CALL EFGP strat
10/21/2017 3:41:05 AMPTA17-0000037904 CALL EFGP strat
10/21/2017 3:42:08 AMMR17-0000001569 CALL EFGP strat
10/21/2017 3:42:44 AMMR17-0000001568 CALL EFGP strat
10/21/2017 3:43:11 AMMR17-0000001568 CALL EFGP strat
10/21/2017 3:43:12 AMPTA17-0000037912 CALL EFGP strat
10/21/2017 3:43:23 AMPTA17-0000037904 CALL EFGP strat
10/21/2017 3:43:51 AMAPP17-0000017880 CALL EFGP strat
10/21/2017 3:46:03 AMAPP17-0000017880 CALL EFGP strat
10/21/2017 3:46:25 AMPTA17-0000037906 CALL EFGP strat
10/21/2017 3:46:38 AMPTA17-0000037910 CALL EFGP strat
10/21/2017 3:46:44 AMPTA17-0000037913 CALL EFGP strat
10/21/2017 3:47:06 AMAPP17-0000017880 CALL EFGP strat
10/21/2017 3:47:07 AMPTA17-0000037906 CALL EFGP strat
10/21/2017 3:48:25 AMPTA17-0000037910 CALL EFGP strat
10/21/2017 3:48:26 AMECN17-0000035187 CALL EFGP strat
10/21/2017 3:48:32 AMPTA17-0000037913 CALL EFGP strat
10/21/2017 3:50:14 AMAPP17-0000017880 CALL EFGP strat
10/21/2017 3:50:34 AMPTA17-0000037904 CALL EFGP strat
10/21/2017 3:50:41 AMPTA17-0000037909 CALL EFGP strat
10/21/2017 3:50:42 AMPTA17-0000037913 CALL EFGP strat
10/21/2017 3:51:38 AMSAP17-0000017299 CALL EFGP strat
10/21/2017 3:52:32 AMAPP17-0000017880 CALL EFGP strat
10/21/2017 3:52:35 AMPTA17-0000037909 CALL EFGP strat
10/21/2017 3:52:41 AMPTA17-0000037904 CALL EFGP strat
10/21/2017 3:53:05 AMPTA17-0000037909 CALL EFGP strat
10/21/2017 3:55:28 AMAPP17-0000017880 CALL EFGP strat
10/21/2017 3:35:52 AMECN17-0000035167 CALL EFGP strat
10/21/2017 3:45:49 AMPTA17-0000037911 CALL EFGP strat
10/21/2017 3:46:38 AMPTA17-0000037914 CALL EFGP strat
10/21/2017 3:47:21 AMECN17-0000035148 CALL EFGP strat
10/21/2017 3:48:27 AMPTA17-0000037914 CALL EFGP strat
10/21/2017 3:49:03 AMPTA17-0000037914 CALL EFGP strat
10/21/2017 3:49:34 AMECN17-0000035148 CALL EFGP strat
10/21/2017 3:51:41 AMPTA17-0000037918 CALL EFGP strat
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭