zoukankan      html  css  js  c++  java
  • C#博客随笔之五:文件格式化输出

    这篇博客主要是用到的知识点是将文件中的内容进行格式化输出oO哦

    直接上图了哈,丑丑哒:

    左边是初始文件呢,右边是结果吼。

    然后上程序界面,变身!!!

    然后接下来呢就是代码啦:

    private void button1_Click(object sender, EventArgs e)
            {
                //读取文件的对话框
                OpenFileDialog fileDialog = new OpenFileDialog();
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    //如果点击确定的话,将文件完整路径存放于textBox1中
                    textBox1.Text = fileDialog.FileName;
                }
            }
    
            private string str = "";
            private void button3_Click(object sender, EventArgs e)
            {
                //将内容全部从文件中读取出来
                using (FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read,FileShare.None))
                {
                    StreamReader sr = new StreamReader(fs);
                    str = sr.ReadToEnd();
                }
                //对数据进行处理
                var res = str.Replace(fromText.Text, toText.Text);
                StreamWriter sw = new StreamWriter(textBox2.Text+"/res.txt", false);
                //然后将数据写入到目标位置
                sw.Write(res);
                sw.Close();
    
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                //选择文件夹对话框,用来选择文件保存的位置
                FolderBrowserDialog fbd = new FolderBrowserDialog();
    
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    textBox2.Text = fbd.SelectedPath;
                }
            }
    

      这篇博客的内容很简单→_→,额Anyway,主要是从文件中读取数据,然后进行处理之后,再将内容写到文件当中, 可用于小量数据的简单处理和格式化,这个会将文件内容全部读取到内存当中的,如果文件内容较大且条数较多的话,可以使用ReadLine哦 ,来进行按行读取,OK,That's all.

    活学活用,have fun!

    么么么哒

  • 相关阅读:
    PsySH——PHP交互式控制台
    PHP通过ssh或socks5读取远程服务器的mysql数据库
    构建:vue项目配置后端接口服务信息
    module.exports用法
    PhpStorm连接服务器,开始自动上传功能
    JavaScript Array.some()方法用法
    vue-router query和params传参(接收参数),$router、$route的区别
    ES6箭头函数(Arrow Functions)
    工作中常用到的ES6语法
    VueJs2.0建议学习路线
  • 原文地址:https://www.cnblogs.com/MelodyWang/p/4440305.html
Copyright © 2011-2022 走看看