this.治疗完毕

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#读取txt文本文件的不同方式

//1.按行读取:每次读取一行内容,即遇到回车键才会停止。

public void readfile(string filepath)
        {
            if (!File.Exists(filepath))
            {
                MessageBox.Show(" file not exits !");
                return;
            }
            string[] result ;
            FileStream fs = new FileStream(filepath, FileMode.Open);
            using (StreamReader sr = new StreamReader(fs, Encoding.Default))
            {
                List<string> list = new List<string>();
                string input;
                    while (!sr.EndOfStream)
                    {
                        input= sr.ReadLine().ToString();
                 
                     }
               } 
           
                sr.Close();
            }




//2. 理论上任意形式的读取方式

using System;

public class SplitTest {
    public static void Main() {

        string words = "This is a list of words, with: a bit of punctuation" +
                       "\tand a tab character.";

        string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });

        foreach (string s in split) {

            if (s.Trim() != "")
                Console.WriteLine(s);
        }
    }
}
// The example displays the following output to the console:
//       This
//       is
//       a
//       list
//       of
//       words
//       with
//       a
//       bit
//       of
//       punctuation
//       and
//       a
//       tab
//       character

 

posted on 2018-05-31 14:48  this.治疗完毕  阅读(120)  评论(0)    收藏  举报