1,读取单个文件
//读取一个文本文件
private void buttonRead_Click(object sender, EventArgs e)
{
String path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/古文观止随声字幕/0101郑伯克段于鄢.lrc";
FileStream fs = new FileStream(path, FileMode.Open);
StreamReader sr = new StreamReader(fs,Encoding.Default);
string line = "";
while ((line=sr.ReadLine())!= "")
{
string substr = line.Substring(line.IndexOf("["), line.LastIndexOf("]") + );//获取文章中[00.00.05]等字符
line = line.Replace(substr, "");//把文章中[00.00.05]字符替换为“”;注意:替换后需要line接受一下,否则无效果
if (line.Length ==) { //判断是否有汉字;
continue;
}
this.textBox1.Text += line+"\r\n";
//MessageBox.Show(i.ToString());
}
sr.Close();
fs.Close();
}
2,读取古文观止文件夹的下所有文件,生成章节目录
///
///
///
private void buttonCreateTitle_Click(object sender, EventArgs e)
{
//1,得到目录下所有文件夹
String path=Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"/古文观止随声字幕";
DirectoryInfo dinfo = new DirectoryInfo(path);
FileInfo [] files=dinfo.GetFiles();//得到目录下的所有文件
int ContengNum = ;//目录编码
foreach (var item in files)
{
//2,开始读文件
FileStream fs = new FileStream(item.FullName,FileMode.Open);//item.fullname获取文件的完整目录
StreamReader sr = new StreamReader(fs,Encoding.Default);//读的时候记得设置一下编码,否则会出乱码
//this.textBox1.Text += sr.ReadLine()+ContengNum+"\r\n";
string str=sr.ReadLine();
//#判断第一行是否
while (str.Equals(""))
{ //用于判断第一行是否为空,取下一行内容
str = sr.ReadLine();
}
this.textBox1.Text += str.Substring(str.LastIndexOf("\]") + ) + "\\t" + ContengNum + "\\r\\n";
sr.Close();
fs.Close();
ContengNum++;
}
}
3,指定目录保存
private void buttonWrite_Click(object sender, EventArgs e)
{
String path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"/1.doc";
MessageBox.Show(path);
FileStream fs = new FileStream(path, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(this.textBox1.Text);
sw.Flush();
sw.Close();
fs.Close();
MessageBox.Show("ok");
}
重点 推荐下面方式
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)
///
///
///
private void button2_Click(object sender, EventArgs e)
{
this.openFileDialog1.ShowDialog();//openFileDialog1 选择的是文件夹的所有文本文件
}
/// <summary>
/// 单击“打开”按钮时触发的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void openFileDialog1\_FileOk(object sender, CancelEventArgs e)
{
String path = this.openFileDialog1.FileName;//获取文件路径
FileStream fs = new FileStream(path, FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
this.textBox1.Text = sr.ReadToEnd();//暂时保存到文本框中
sr.Close();
fs.Close();
}
3,多线程读取文件(无法使用上面的this.openfiledialog)-----待修改
private void button4_Click(object sender, EventArgs e)
{
ThreadStart st = new ThreadStart(this.openfolder);//开始方法
//ThreadStart st = new ThreadStart(this.temptest);// 测试ok
Thread t = new Thread(st);
Control.CheckForIllegalCrossThreadCalls=false;//使得子线程可以访问主线程创建的方法
t.Start();
}
小结一下:
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,赋值后给子线程用;
String fpath = "";//定义全局变量供子线程使用;
public Form1()
{
InitializeComponent();
}
///
///
///
private void button4_Click(object sender, EventArgs e)
{
this.folderBrowserDialog1.ShowDialog();
fpath = this.folderBrowserDialog1.SelectedPath;//赋值给全局变量fpath,供子线程使用;
ThreadStart st = new ThreadStart(this.openfolder);//开始方法
Thread t = new Thread(st);
//使得子线程可以访问主线程创建的方法
t.Start();
}
本人菜鸟,老鸟勿喷;
但是可以指点一下,有助菜鸟成长
手机扫一扫
移动阅读更方便
你可能感兴趣的文章