C#读取文件-古文观止(总结一下)
1,读取单个文件
1 //读取一个文本文件 2 private void buttonRead_Click(object sender, EventArgs e) 3 { 4 String path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/古文观止随声字幕/0101郑伯克段于鄢.lrc"; 5 FileStream fs = new FileStream(path, FileMode.Open); 6 StreamReader sr = new StreamReader(fs,Encoding.Default); 7 string line = ""; 8 while ((line=sr.ReadLine())!= "") 9 { 10 string substr = line.Substring(line.IndexOf("["), line.LastIndexOf("]") + 1);//获取文章中[00.00.05]等字符 11 line = line.Replace(substr, "");//把文章中[00.00.05]字符替换为“”;注意:替换后需要line接受一下,否则无效果 12 if (line.Length ==0) { //判断是否有汉字; 13 continue; 14 } 15 this.textBox1.Text += line+"\r\n"; 16 17 //MessageBox.Show(i.ToString()); 18 } 19 sr.Close(); 20 fs.Close(); 21 22 }
2,读取古文观止文件夹的下所有文件,生成章节目录
1 /// <summary> 2 /// 生成章节目录,取文件夹的第一行内容 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void buttonCreateTitle_Click(object sender, EventArgs e) 7 { 8 //1,得到目录下所有文件夹 9 String path=Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"/古文观止随声字幕"; 10 DirectoryInfo dinfo = new DirectoryInfo(path); 11 FileInfo [] files=dinfo.GetFiles();//得到目录下的所有文件 12 int ContengNum = 1;//目录编码 13 foreach (var item in files) 14 { 15 //2,开始读文件 16 FileStream fs = new FileStream(item.FullName,FileMode.Open);//item.fullname获取文件的完整目录 17 StreamReader sr = new StreamReader(fs,Encoding.Default);//读的时候记得设置一下编码,否则会出乱码 18 //this.textBox1.Text += sr.ReadLine()+ContengNum+"\r\n"; 19 string str=sr.ReadLine(); 20 21 //#判断第一行是否 22 while (str.Equals("")) 23 { //用于判断第一行是否为空,取下一行内容 24 str = sr.ReadLine(); 25 } 26 this.textBox1.Text += str.Substring(str.LastIndexOf("]") + 1) + "\t" + ContengNum + "\r\n"; 27 sr.Close(); 28 fs.Close(); 29 ContengNum++; 30 } 31 }
3,指定目录保存
1 private void buttonWrite_Click(object sender, EventArgs e) 2 { 3 String path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"/1.doc"; 4 MessageBox.Show(path); 5 FileStream fs = new FileStream(path, FileMode.Create); 6 StreamWriter sw = new StreamWriter(fs); 7 sw.Write(this.textBox1.Text); 8 sw.Flush(); 9 sw.Close(); 10 fs.Close(); 11 MessageBox.Show("ok"); 12 }
重点 推荐下面方式
1,使用控件读取文件夹下文本文件(folderBrowserDialog)
//用于读取
public void openfolder() {
folderBrowserDialog1.ShowDialog(); //子线程无法调用, 估计 this原因
string path = this.folderBrowserDialog1.SelectedPath;//得到文件夹的路径
DirectoryInfo dinfo = new DirectoryInfo(path);
FileInfo[] files = dinfo.GetFiles();
foreach (var item in files)
{
FileStream fs = new FileStream(item.FullName, FileMode.Open);
StreamReader sr = new StreamReader(fs,Encoding.Default);
this.textBox1.Text+= sr.ReadToEnd();//一行一行读得慢,在此使用readtoend;也可以使用readtoline
sr.Close();
fs.Close();
}
}
2,使用控件直接读取文本文件(openFiledialog)
1 /// <summary> 2 /// openFileDialog1控件选择文件夹 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void button2_Click(object sender, EventArgs e) 7 { 8 this.openFileDialog1.ShowDialog();//openFileDialog1 选择的是文件夹的所有文本文件 9 10 } 11 /// <summary> 12 /// 单击“打开”按钮时触发的事件 13 /// </summary> 14 /// <param name="sender"></param> 15 /// <param name="e"></param> 16 private void openFileDialog1_FileOk(object sender, CancelEventArgs e) 17 { 18 String path = this.openFileDialog1.FileName;//获取文件路径 19 FileStream fs = new FileStream(path, FileMode.Open); 20 StreamReader sr = new StreamReader(fs, Encoding.Default); 21 this.textBox1.Text = sr.ReadToEnd();//暂时保存到文本框中 22 sr.Close(); 23 fs.Close(); 24 }
3,多线程读取文件(无法使用上面的this.openfiledialog)-----待修改
1 private void button4_Click(object sender, EventArgs e) 2 { 3 ThreadStart st = new ThreadStart(this.openfolder);//开始方法 4 //ThreadStart st = new ThreadStart(this.temptest);// 测试ok 5 Thread t = new Thread(st); 6 Control.CheckForIllegalCrossThreadCalls=false;//使得子线程可以访问主线程创建的方法 7 t.Start(); 8 }
小结一下:
1,直接读文本文件: 创建文件流--》读写流--》读写操作--》0k
a, String path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/古文观止随声字幕/0101郑伯克段于鄢.lrc";
FileStream fs = new FileStream(path, FileMode.Open);
StreamReader sr = new StreamReader(fs,Encoding.Default); //注意字符编码
sr.readToEnd(); //或者sr.readToLine();
sr.close();
fs.close();
b, String path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"/1.doc";
FileStream fs = new FileStream(path, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(this.textBox1.Text);
sw.Flush(); //写的时候flush一下
sw.Close();
fs.Close();
MessageBox.Show("ok");
2,读文件夹 : 获取目录--》创建文件流--》读写流--》读写操作--》0k
String path=Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"/古文观止随声字幕";
DirectoryInfo dinfo = new DirectoryInfo(path);
FileInfo [] files=dinfo.GetFiles()
foreach (var item in files)
{
FileStream fs = new FileStream(item.FullName,FileMode.Open);//item.fullname获取文件的完整目录
StreamReader sr = new StreamReader(fs,Encoding.Default);
string str=sr.ReadLine();
this.textBox1.Text+=str+"\r\n";
sr.Close();
fs.Close()
}
3, fodlerBrowserDialog读取文件夹;openFileDialog读取文件,点击“打开\保存”时,触发fileOk事件;
4,上述多线程问题解决:(注意:子线程只用结果,而不调用主线程创建的组件;)
1,主线程创建全局变量fpath;
2,赋值后给子线程用;
1 String fpath = "";//定义全局变量供子线程使用; 2 public Form1() 3 { 4 InitializeComponent(); 5 } 6 /// <summary> 7 /// 多线程问题:子线程只用;而不调用主线程创建的组件 8 /// </summary> 9 /// <param name="sender"></param> 10 /// <param name="e"></param> 11 private void button4_Click(object sender, EventArgs e) 12 { 13 this.folderBrowserDialog1.ShowDialog(); 14 fpath = this.folderBrowserDialog1.SelectedPath;//赋值给全局变量fpath,供子线程使用; 15 ThreadStart st = new ThreadStart(this.openfolder);//开始方法 16 Thread t = new Thread(st); 17 //使得子线程可以访问主线程创建的方法 18 t.Start(); 19 }
本人菜鸟,老鸟勿喷;
但是可以指点一下,有助菜鸟成长