欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c#学习之TXT文件的读取与写入

程序员文章站 2022-06-13 08:01:06
...

TXT文件读取

1.使用File类

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

namespace txtfile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //读取文件
        private void button2_Click(object sender, EventArgs e)
        {
            //全部读取1
            string str = File.ReadAllText(textBox1.Text, Encoding.Default);
            textBox2.AppendText(str + "\n");  

            //全部读取2
            string[] str1 = File.ReadAllLines(textBox1.Text, Encoding.Default);
            //遍历方式1
            foreach(string r in str1)
            {
                textBox2.AppendText(r + "\n");
            }
            //遍历方式2
            /*
            for (int i = 0;i<str1.Length ; i++)
            {
                textBox2.AppendText(str1[i] + "\n");
            }
            */   
        }
    }
}

 

2.使用StreamReader类

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

namespace txtfile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //读取文件
        private void button2_Click(object sender, EventArgs e)
        {
            //全部读取
            FileStream fs = new FileStream(textBox1.Text, FileMode.Open);
            StreamReader sr = new StreamReader(fs, Encoding.Default);
            string str = sr.ReadToEnd();

            //逐行读取
            string str1;
            while((str1 = sr.ReadLine()) != null)
            {
                textBox2.AppendText(str + "\n");
            }

            fs.Close();
            sr.Close();

        }
    }
}

TXT文件写入

1.使用FileStream类

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

namespace txtfile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //文件写入
        private void button3_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("D:\\test2.txt", FileMode.OpenOrCreate);
            byte[] data = Encoding.Default.GetBytes("你好!Hello World!");
            fs.Write(data, 0, data.Length);
            fs.Flush();
            fs.Close();

        }
    }
}

2.使用StreamWriter类

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

namespace txtfile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //文件写入
        private void button3_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("D:\\test2.txt", FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs, Encoding.Default);
            sw.Write("你好!Hello World!");
            sw.Flush();
            sw.Close();
            fs.Close();

        }
    }
}

​