C#对各种文件的操作-txt(1)


C#对txt文件的操作

  由于工作中需要对各种文件进行解析,读写操作,结合网上的一些比较好的方法。总结一套C#对各种文件的操作。

A . 写入和读取txt文件,首先对文件和文件进行判断,如果文件或文件夹存在或不存在,都有不同的解决办法。读写txt文件,下面代码显示了两种方法,分别是直接操作和以流的方式操作

关键类:Directory,  File, StreamWriter, StreamReader

关键命名空间:System.IO

个人觉得,命名空间和类在学习一个新的方法时是非常重要的,在VS上面引入命名空间,阅读方法的详细注释和官方文档,再做一两个demo应该是最快的捷径了。

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using System.IO;
  7 
  8 namespace TxtWriteAndRead
  9 {
 10     class Program
 11     {
 12         static void Main(string[] args)
 13         {
 14             string str = @"C:\Users\wuxinlong\Desktop\demo\txt\testFile";
 15             Program Function = new Program();
 16             //Function.TextWrite(str);
 17             Function.TextRead(str);
 18             Console.ReadKey();
 19         }
 20 
 21         /// <summary>
 22         /// 写文本文件,换行,不换行,以流的方式写数据
 23         /// </summary>
 24         public void TextWrite(string str)
 25         {            
 26             if (Directory.Exists(str) == false) //如果不存在就创建file文件夹
 27             {
 28                 Directory.CreateDirectory(str);
 29             }
 30             //Directory.Delete(@"C:\Users\wuxinlong\Desktop\demo\博客园测试\testFile");//删除空的目录
 31             //Directory.Delete(@"C:\Users\wuxinlong\Desktop\demo\博客园测试\testFile", true);//删除目录及目录下的所有文件夹和文件
 32 
 33             //判断文件的存在
 34             if (File.Exists(str + "\\Test.txt"))
 35             {
 36                 Console.WriteLine("该文件已经存在");
 37                 Console.ReadKey();
 38             }
 39             else
 40             {
 41                 //如果文件不存在,则创建该文件
 42                 File.Create(str + "\\Test.txt");
 43             }
 44 
 45             //写入字符串数组,并且换行
 46             string[] lines = { "first line", "second line", "third line", "第四行" };
 47             //如果该文件存在,并向其中追加写入数据
 48             if (File.Exists(str + "\\Test.txt"))
 49             {
 50                 File.AppendAllLines(str + "\\Test.txt", lines, Encoding.UTF8);
 51             }
 52             else
 53             //如果该文件不存在,则创建该文件,写入数据
 54             {
 55                 //如果该文件存在,这个方法会覆盖该文件中的内容
 56                 File.WriteAllLines(str + "\\Test.txt", lines, Encoding.UTF8);
 57             }
 58 
 59             //如果文件不存在,则创建;存在则覆盖
 60             string strTest = "测试一个字符串写入文本文件,并且覆盖";
 61             System.IO.File.WriteAllText(str + "\\Test.txt", strTest, Encoding.UTF8);
 62 
 63             //以流的方式写数据到文本文件中
 64             using (StreamWriter file = new StreamWriter(str + "\\Test.txt", true)) //如果该文件存在,则覆盖
 65             {
 66                 foreach (string line in lines)
 67                 {
 68                     if (!line.Contains("second"))
 69                     {
 70                         file.Write(line); //追加文件末尾,不换行
 71                         file.WriteLine(line); //追加文件末尾,换行
 72                     }
 73                 }
 74             }
 75         }
 76 
 77         public void TextRead(string str)
 78         {
 79             //直接读取出字符串
 80             string text = File.ReadAllText(str + "\\Test.txt");
 81             Console.WriteLine(text);
 82 
 83             //按行读取为字符串,转换为数组
 84             string[] lines = File.ReadAllLines(str + "\\Test.txt");
 85             foreach (string line in lines)
 86             {
 87                 Console.WriteLine(line);
 88             }
 89 
 90             //从头到尾以流的方式读出文本文件
 91             //该方法会一行一行读出文本
 92             using (StreamReader sr = new StreamReader(str + "\\Test.txt"))
 93             {
 94                 string strs;
 95                 while ((strs = sr.ReadLine()) != null)
 96                 {
 97                     Console.WriteLine(strs);
 98                 }
 99             }
100             Console.Read();
101 
102         }
103     }
104 }

 

 

 

  

 

 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM