标签:
这篇博客主要是用到的知识点是将文件中的内容进行格式化输出
直接上图
左边是初始文件,右边是结果。
然后上程序界面

然后接下来上代码
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;
}
}
这篇博客的内容很简单,主要是从文件中读取数据,然后进行处理之后,再将内容写到文件当中, 可用于小量数据的简单处理和格式化,这个会将文件内容全部读取到内存当中的,如果文件内容较大且条数较多的话,可以使用ReadLine,来进行按行读取
标签:
原文地址:http://www.cnblogs.com/MelodyWang/p/4440305.html