基于C#实现文本读取的7种方式

前言

文本读取在上位机开发中经常会使用到,实现的方式也有很多种,今天跟大家分享一下C#实现读取读取的7种方式。

这里我们先写好了一个测试界面,提供一个文件路径选择的入口,具体如下所示:.

基于C#实现文本读取的7种方式

方式一

基于FileStream,并结合它的Read方法读取指定的字节数组,最后转换成字符串进行显示。

            this.rtb_Content.Clear();            FileStream fs = new FileStream(this.txt_FilePath.Text, FileMode.Open, FileAccess.Read);            int n = (int)fs.Length;            byte[] b = new byte[n];            int r = fs.Read(b, 0, n);            fs.Close();            this.rtb_Content.Text = Encoding.UTF8.GetString(b, 0, n);

方式二

基于FileStream,一个字节一个字节读取,放到字节数组中,最后转换成字符串进行显示。

            this.rtb_Content.Clear();            FileStream fs = new FileStream(this.txt_FilePath.Text, FileMode.Open, FileAccess.Read);            long n = fs.Length;            byte[] b = new byte[n];            int data, index;            index = 0;            data = fs.ReadByte();            while (data != -1)            {                b[index++] = Convert.ToByte(data);                data = fs.ReadByte();            }            fs.Close();            this.rtb_Content.Text = Encoding.UTF8.GetString(b);

方式三

基于File类,直接全部读取出来并显示。

            this.rtb_Content.Clear();            this.rtb_Content.Text = File.ReadAllText(this.txt_FilePath.Text, Encoding.UTF8);

方式四

基于StreamReader,一行一行读取,最后拼接并显示。

            this.rtb_Content.Clear();            StreamReader sr = new StreamReader(this.txt_FilePath.Text, Encoding.UTF8);            string line = sr.ReadLine();            while (line != null)            {                this.rtb_Content.AppendText(line);                line = sr.ReadLine();                if (line != null)                {                    this.rtb_Content.AppendText("\r\n");                }            }            sr.Close();

方式五

基于StreamReader,一次性读取到结尾,最后显示。

            this.rtb_Content.Clear();            StreamReader sr = new StreamReader(this.txt_FilePath.Text, Encoding.UTF8);            this.rtb_Content.Text = sr.ReadToEnd();            sr.Close();

方式六

基于StreamReader,一行一行读取,通过EndOfSteam判断是否到结尾,最后拼接并显示。

            this.rtb_Content.Clear();            StreamReader sr = new StreamReader(this.txt_FilePath.Text, Encoding.UTF8);            while (!sr.EndOfStream)            {                this.rtb_Content.AppendText(sr.ReadLine());                if (!sr.EndOfStream)                {                    this.rtb_Content.AppendText("\r\n");                }            }            sr.Close();

方式7

基于FileStream和StreamReader来实现。

            this.rtb_Content.Clear();            FileStream fs = new FileStream(this.txt_FilePath.Text, FileMode.Open, FileAccess.Read);            StreamReader sr = new StreamReader(fs, Encoding.UTF8);            this.rtb_Content.Text = sr.ReadToEnd();            fs.Close();            sr.Close();

测试结果

经过测试,以上每个方法都可以实现文本文件的读取。

基于C#实现文本读取的7种方式

总结

以上7种方式主要是分别基于FileStream、File和StreamReader这三种来实现的,这三种方式的区别在于:

  • FileStream类可以对任意类型的文件进行读取操作,而且我们也可以按照需要指定每一次读取字节长度,以此减少内存的消耗,提高读取效率。

  • StreamReader的特点是,它只能对文本文件进行读写操作,可以一行一行的写入和读取。

  • File类它是一个静态类,当我们查看file类的那些静态方法时,我们可以发现,在这个类里面的方法封装了可以执行文件读写操作的对象,例如:Filestream,StreamReader,我们通过File去执行任何文件的读写操作时,实际上是使用FileStream或SteamReader对象来执行文件的读写操作,代码如下所示:

        public static string ReadAllText(string path, Encoding encoding)        {            if (path == null)            {                throw new ArgumentNullException("path");            }            if (encoding == null)            {                throw new ArgumentNullException("encoding");            }            if (path.Length == 0)            {                throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));            }            return InternalReadAllText(path, encoding, checkHost: true);        }                private static string InternalReadAllText(string path, Encoding encoding, bool checkHost)        {            using (StreamReader streamReader = new StreamReader(path, encoding, detectEncodingFromByteOrderMarks: true, StreamReader.DefaultBufferSize, checkHost))            {                return streamReader.ReadToEnd();            }        }