基于C#实现文本读取的方法

这里介绍文本读取常用的方法,主要是采用FileStream或StreamReader进行文件读取,使用的界面如下:

 

 代码如下:

using System;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace 读取text文件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }
        OpenFileDialog ofd;
        private void button1_Click(object sender, EventArgs e)
        {
            ofd = new OpenFileDialog();            
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = ofd.FileName;
            }
        }

        private void btnReadFile1_Click(object sender, EventArgs e)
        {
            //基于FileStream,并结合它的Read方法读取指定的字节数组,最后转换成字符串进行显示。
            this.richTextBox1.Clear();
            FileStream fs = new FileStream(this.textBox1.Text, FileMode.Open, FileAccess.Read);
            int n = (int)fs.Length;
            byte[] buffer = new byte[n];
            fs.Read(buffer, 0, n);
            fs.Close();
            this.richTextBox1.Text = Encoding.UTF8.GetString(buffer, 0, n);
        }

        private void btnReadFile2_Click(object sender, EventArgs e)
        {
            //基于FileStream,一个字节一个字节读取,放到字节数组中,最后转换成字符串进行显示。
            this.richTextBox1.Clear();
            FileStream fs = new FileStream(this.textBox1.Text, FileMode.Open, FileAccess.Read);
            int n = (int)fs.Length;
            byte[] bytes = new byte[n];
            int data, index = 0;
            data = fs.ReadByte();//每次读一个byte,读到末尾数时为-1
            while (data != -1)
            {
                bytes[index++] = (byte)data;
                data = fs.ReadByte();
            }
            fs.Close();
            this.richTextBox1.Text = Encoding.UTF8.GetString(bytes);
        }

        private void btnReadFile3_Click(object sender, EventArgs e)
        {
            //基于File类,直接全部读取出来并显示。
            this.richTextBox1.Clear();
            this.richTextBox1.Text = File.ReadAllText(this.textBox1.Text, Encoding.UTF8);
        }

        private void btnReadFile4_Click(object sender, EventArgs e)
        {
            //基于StreamReader,一次性读取到结尾,最后显示。
            this.richTextBox1.Clear();
            StreamReader sr = new StreamReader(this.textBox1.Text, Encoding.UTF8);
            string str = sr.ReadToEnd();
            sr.Close();
            this.richTextBox1.Text = str;
        }

        private void btnReadFile5_Click(object sender, EventArgs e)
        {
            //基于StreamReader,一行一行读取,最后拼接并显示。
            this.richTextBox1.Clear();
            StreamReader sr = new StreamReader(this.textBox1.Text, Encoding.UTF8);
            string str = sr.ReadLine();
            while (str != null)
            {
                this.richTextBox1.AppendText(str);
                str = sr.ReadLine();
                if (str != null)
                {
                    this.richTextBox1.AppendText("\r\n");//每读完一行数据要进行回车换行
                }
            }
            sr.Close();
        }

        private void btnReadFile6_Click(object sender, EventArgs e)
        {
            //基于StreamReader,一行一行读取,通过EndOfSteam判断是否到结尾,最后拼接并显示。
            this.richTextBox1.Clear();
            StreamReader sr = new StreamReader(this.textBox1.Text, Encoding.UTF8);
            while (!sr.EndOfStream)
            {
                this.richTextBox1.AppendText(sr.ReadLine());
                if (!sr.EndOfStream)
                {
                    this.richTextBox1.AppendText("\r\n");
                }
            }
            sr.Close();
        }

        private void btnReadFile7_Click(object sender, EventArgs e)
        {
            //基于FileStream和StreamReader来实现。
            this.richTextBox1.Clear();
            FileStream fs = new FileStream(this.textBox1.Text, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs, Encoding.UTF8);
            this.richTextBox1.Text = sr.ReadToEnd();
            fs.Close();
            sr.Close();
        }
    }
}

 

posted on 2022-11-25 11:52  hanzq_go  阅读(1418)  评论(0编辑  收藏  举报

导航