C#控制台控制台将一个文件夹中的所有txt文件中的数据读取出来

 最近老师布置的数据挖掘大作业,要用到很多txt文件数据,从网上找了很久,找到这个批量读取txt文件的方法。

 1 static void Main(string[] args)
 2         {
 3             System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("d:\\loandb");
 4             foreach (System.IO.FileInfo fi in dir.GetFiles())
 5             {
 6                 if (fi.Extension == ".txt")
 7                 {
 8                     System.IO.StreamReader sr = new System.IO.StreamReader(fi.FullName);
 9                     string content = sr.ReadToEnd();
10                     foreach (string s in content.Split('\n'))
11                         Console.WriteLine(s);
12                     sr.Close();
13                 }
14             }

15         }

posted on 2012-12-17 22:01  Gulibao  阅读(4072)  评论(0编辑  收藏  举报

导航