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!
么么么哒
浙公网安备 33010602011771号