0
点赞
收藏
分享

微信扫一扫

VS2008读写文本文件(控制)及处理策略(实例)


一、实例窗体

 

二、运行结果图

三、实力源码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; 
 
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace file_w_r
{
    public partial class Form1 : Form
    {
        string[] str_tt = new string[3000]; //测试数组,存放第一次读取的内容
        string[] str_tu = new string[3000]; //测试数组,存放第二次读取的内容
        int kk;//数组控制变量
        string path;
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            if (saveFile_Dialog.ShowDialog() == DialogResult.OK)
            {
                path = saveFile_Dialog.FileName;                FileWriter(path, this.textBox1.Text);
            }
        }        private void button2_Click(object sender, EventArgs e)
        {
            if (openFile_Dialog.ShowDialog() == DialogResult.OK)
            {
                path = openFile_Dialog.FileName;
                string str = FileReader(path,str_tt );
                this.textBox2.Text = str;
            }
        }
        /// <summary>
        /// 读取文件内容,需要文件路径,可以使用
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public string FileReader(string filepath,string[] str_arr)
        {            StreamReader sr = null;
            string FileText = string.Empty;
            int i = 0;
            try
            {
                if (File.Exists(filepath))
                {
                    sr = new StreamReader(filepath, Encoding.GetEncoding("gb2312"));//以gb2312字符编码格式写文本。
                    FileText = sr.ReadToEnd();
                }
                sr.BaseStream.Seek(0, SeekOrigin.Begin);//文件回到起始位置
                string str_t ;//= sr.ReadLine();
                label1.Text = "";
                while ((str_t = sr.ReadLine()) != null)//读取文本中的一行
                {
                    str_arr[i] = str_t ;//按顺序赋值给字符串数组
                    label1.Text = label1.Text +"\n"+ str_arr[i];
                    i++;
                    kk = i;
                }
                sr.Close();
            }
            catch
            {
                //MessageBox.Show(ex.Message);
            }
            finally
            {            }
            return FileText;
        }        /// <summary>
        /// 文件写入
        /// </summary>
        /// <param name="filepath">文件路径</param>
        /// <param name="filetext">文件内容</param>
        public void FileWriter(string filepath, string filetext)
        {            StreamWriter sw = null;
            try
            {
                if (File.Exists(filepath))
                {
                    File.Delete(filepath);
                }
                sw = File.CreateText(filepath);
                sw.Write(filetext, false, Encoding.GetEncoding("gb2312"));
                sw.Flush();
            }
            catch
            {
                //MessageBox.Show(ex.Message);
            }
            finally
            {
                sw.Close();
            }
        }        private void button3_Click(object sender, EventArgs e)//读取第二个文件
        {
            if (openFile_Dialog.ShowDialog() == DialogResult.OK)
            {
                //str_tu = str_tt;
                path = openFile_Dialog.FileName;
                string str = FileReader(path,str_tu );
                this.textBox1.Text = str;
                label2.Text = str_tu[1]+"---"+str_tt [1];
            }
        }        private void button4_Click(object sender, EventArgs e)//合并文件
        {
            textBox1.Text =  "";
            label2.Text = "";
            for (int i = 0; i < kk; i++)
            {
                if (str_tt[i] == str_tu[i])
                {
                    label2.Text = label2.Text + "\r\n" + str_tu[i];
                    textBox1.Text = label2 .Text;
                }
                else
                {
                    str_tu[i] = str_tu[i] + "\r\n" + str_tt[i];
                    label2.Text = label2.Text + "\r\n" + str_tu[i];
                    textBox1.Text = label2 .Text ;
                }
            }
            if (saveFile_Dialog.ShowDialog() == DialogResult.OK)
            {
                path = saveFile_Dialog.FileName;                FileWriter(path, label2.Text );
            }
        }
    }
}

举报

相关推荐

0 条评论