.Net C# Txt文本文件操作

原创 2017年07月25日 14:29:25

写入

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

深入.NET平台和C#编程

-
  • 1970年01月01日 08:00

asp.net写入日志到文本文件

reference: http://blog.csdn.net/lz00728/article/details/7477910 using System;   using...
  • u010032207
  • u010032207
  • 2017-02-13 18:51:07
  • 1312

C# 对文本文件的几种读写方法总结

计算机在最初只支持ASCII编码,但是后来为了支持其他语言中的字符(比如汉字)以及一些特殊字符(比如€),就引入了Unicode字符集。基于Unicode字符集的编码方式有很多,比如UTF-7、UTF...
  • tchenjiant
  • tchenjiant
  • 2015-10-27 09:15:31
  • 11249

C# .Net读写文件 IO流 C# .Net对文件&文件夹操作

通常来讲,用C#程序读取一个文件需要以下5个基本步骤 1:创建文件流 2:创建阅读器或者写入器 3:执行读写操作 4:关闭阅读器或者写入器 5:关闭文件流 (注意事项: 使用文件流需要导入...
  • miniduhua
  • miniduhua
  • 2016-10-21 15:16:42
  • 4967

asp.net 文件操作

在ASP.NET中,文件处理的整个过程都是围绕着System.IO 这个名称空间展开的。这个名称空间中具有执行文件读、写所需要的类。Directory用于创建、移动和枚举通过目录和子目录,File用于...
  • zhuyu19911016520
  • zhuyu19911016520
  • 2015-04-23 09:09:13
  • 1472

.net 文件操作类(转载)

using System; using System.Collections.Generic; using System.Text; using System.IO; #endregi...
  • anfeng_8379
  • anfeng_8379
  • 2012-01-13 11:11:38
  • 4363

C#读写txt文件的两种方法介绍

1.添加命名空间   System.IO;   System.Text; 2.文件的读取   (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输...
  • ybhjx
  • ybhjx
  • 2016-12-17 13:59:34
  • 18915

C# 如何创建与读写txt文件

// 判断文件是否存在,不存在则创建,否则读取值显示到窗体             if (!File.Exists("F:\\TestTxt.txt"))             {     ...
  • fuzhenglai
  • fuzhenglai
  • 2016-11-14 13:49:15
  • 20345

C#文本文件操作

如何向现有文件中添加文本using System;using System.IO;class Test {    public static void Main()     {        // C...
  • 21aspnet
  • 21aspnet
  • 2007-03-20 21:37:00
  • 3347

C#将文本框中的字符串导出至txt文件中

using System.IO;   System.Windows.Forms.SaveFileDialog objSave = new System.Windows.Forms.SaveFile...
  • gaoxiang19820514
  • gaoxiang19820514
  • 2011-11-03 19:03:59
  • 10161
收藏助手
不良信息举报
您举报文章:.Net C# Txt文本文件操作
举报原因:
原因补充:

(最多只允许输入30个字)