C# WinForm中有关txt文件读取的问题
例如:有个文本文件内容如下:XXXXXXXXXXXXaaaXXXXXXXXXXXXbbbXXXXXXXXXXXXXXXXXXaaaXXXXXXXXXXcccXXXXXXX...
例如:有个文本文件内容如下:
XXXXXXXXXXXXaaa
XXXXXXXXXXXXbbb
XXXXXXXXXXXXXXXXXXaaaXXX
XXXXXXX ccc
XXXXXXXXXXXXX aaa
如何取出上面文本中带aaa的行,以及有多少行,在textbox中显示出来。
希望能给出代码,先谢谢了! 展开
XXXXXXXXXXXXaaa
XXXXXXXXXXXXbbb
XXXXXXXXXXXXXXXXXXaaaXXX
XXXXXXX ccc
XXXXXXXXXXXXX aaa
如何取出上面文本中带aaa的行,以及有多少行,在textbox中显示出来。
希望能给出代码,先谢谢了! 展开
2个回答
展开全部
用流可以一行一行的读。每读到一行++,就行了!
using System.IO;
using System.Text;
int count = 0;
FileStream fs = new FileStream("E:\\1.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("GB2312"));
string line = sr.ReadLine();
for (int i = 0; line != null; i++)
{
if (line.Contains("a") == true)
{
count++;
TextBox1.Text += line;
}
line = sr.ReadLine();
}
Response.Write(count);
sr.Close();
fs.Close();
不懂在HI我吧
using System.IO;
using System.Text;
int count = 0;
FileStream fs = new FileStream("E:\\1.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("GB2312"));
string line = sr.ReadLine();
for (int i = 0; line != null; i++)
{
if (line.Contains("a") == true)
{
count++;
TextBox1.Text += line;
}
line = sr.ReadLine();
}
Response.Write(count);
sr.Close();
fs.Close();
不懂在HI我吧
展开全部
OpenFileDialog loOpenFile = new OpenFileDialog();
if (loOpenFile.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(loOpenFile.FileName,FileMode.Open);
StreamReader sr = new StreamReader(fs);
string an=sr.ReadLine();
int tatle = 0;
while (an!= null)
{
if(an.IndexOf("a")>0)
{
TextBox1.Text = TextBox1.Text + an + "\n";
tatle++;
}
an = sr.ReadLine();
}
sr.Close();
fs.Close();
this.Text = "行含有a字母的行一共有"tatle+"行"
}
if (loOpenFile.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(loOpenFile.FileName,FileMode.Open);
StreamReader sr = new StreamReader(fs);
string an=sr.ReadLine();
int tatle = 0;
while (an!= null)
{
if(an.IndexOf("a")>0)
{
TextBox1.Text = TextBox1.Text + an + "\n";
tatle++;
}
an = sr.ReadLine();
}
sr.Close();
fs.Close();
this.Text = "行含有a字母的行一共有"tatle+"行"
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询