主题:用C#读取文本文件中指定的行
supercrsky
[专家分:580] 发布于 2007-02-22 10:32:00
用C#读取文本文件中指定的行
怎么实现?
最好给个例子,谢谢。
回复列表 (共6个回复)
沙发
duckzq [专家分:60] 发布于 2007-02-22 22:10:00
你是想实现你前面那个问题中的查找功能吧?!
改改文件排版格式就没有这种问题了。
private void button2_Click(object sender, System.EventArgs e)
{
StreamWriter writer = new StreamWriter(@"c:\Customer.txt",true,Encoding.Default);
//所有信息在一行,,查找时遍历一次文件每行的第一个字段就行了。
//或者你用xml格式的文件保存,查找更方便。。
writer.Write(this.txtName.Text+"\t");//"\t"是制表符,,分隔符的一种
writer.Write(this.txtPhone.Text+"\t");
writer.WriteLine(this.txtAddress.Text);
writer.Close();
MessageBox.Show("追加成功","提示");
this.txtName.Clear();
this.txtAddress.Clear();
this.txtPhone.Clear();
this.txtName.Focus();
}
板凳
supercrsky [专家分:580] 发布于 2007-02-23 07:18:00
[quote]你是想实现你前面那个问题中的查找功能吧?!
改改文件排版格式就没有这种问题了。
private void button2_Click(object sender, System.EventArgs e)
{
StreamWriter writer = new StreamWriter(@"c:\Customer.txt",true,Encoding.Default);
//所有信息在一行,,查找时遍历一次文件每行的第一个字段就行了。
//或者你用xml格式的文件保存,查找更方便。。
writer.Write(this.txtName.Text+"\t");//"\t"是制表符,,分隔符的一种
writer.Write(this.txtPhone.Text+"\t");
writer.WriteLine(this.txtAddress.Text);
writer.Close();
MessageBox.Show("追加成功","提示");
this.txtName.Clear();
this.txtAddress.Clear();
this.txtPhone.Clear();
this.txtName.Focus();
}
[/quote]
其实这道题的Customer.txt是有格式要求的:
电话: 464643131
姓名: 杨可
地址: 河北
就像上面这样,要求必须把姓名放在第二行。这样的话怎么实现?
3 楼
duckzq [专家分:60] 发布于 2007-02-24 00:51:00
public ArrayList readTxtFile(string path,string fgf)
{
if (!File.Exists(path))
{
Console.WriteLine("文件不存在!");
Console.WriteLine("按回车键退出!");
Console.ReadLine();
return null;
}
try
{
//读出一行文本,并临时存放在ArrayList中
StreamReader sr = new StreamReader(path, Encoding.GetEncoding("gb2312"));
string l;
ArrayList content = new ArrayList();
ArrayList row = new ArrayList();
int i = 0;
while((l = sr.ReadLine()) != null)
{
l = l.Trim();
row.Add(l);
if(Math.IEEERemainder((i-2),3)==0)//取余函数,被3整除。
{
row.TrimToSize();
content.Add(row);
row.Clear();
}
i++;
}
sr.Close();
content.TrimToSize();
return content;
}
catch(IOException ex)
{
Console.WriteLine("读文件出错!请检查文件是否正确。");
Console.WriteLine(ex.ToString());
return null;
}
}
4 楼
duckzq [专家分:60] 发布于 2007-02-24 00:59:00
我写过一个其他格式文本文件读取的函数。。下面列出来,你可以看看对你有没有用;
//用ArrayList必须加这句;
using System.Collections;
//读文件;
//path 工作目录; fgf 分隔符;
public ArrayList readTxtFile(string path,string fgf)
{
if (!File.Exists(path))
{
Console.WriteLine("文件不存在!");
Console.WriteLine("按回车键退出!");
Console.ReadLine();
return null;
}
try
{
//读出一行文本,并临时存放在ArrayList中
StreamReader sr = new StreamReader(path, Encoding.GetEncoding("gb2312"));
string l;
ArrayList content = new ArrayList();
while((l = sr.ReadLine()) != null)
{
ArrayList row = new ArrayList();
l = l.Trim();
int length = l.Length;
int i = 0;
bool fgf_flag = false;
while(i < length)
{
string x = l[i].ToString();
i++;
if (x.Equals(fgf))
{
fgf_flag = true;
row.Add(l.Substring(0,i-1));
length = length-i+1;
i = 0;
}
}
if (!fgf_flag)
row.Add(l);
row.TrimToSize();
//将多行文本储存在ArrayList中,并返回;
content.Add(row);
}
sr.Close();
content.TrimToSize();
return content;
}
catch(IOException ex)
{
Console.WriteLine("读文件出错!请检查文件是否正确。");
Console.WriteLine(ex.ToString());
return null;
}
}
5 楼
duckzq [专家分:60] 发布于 2007-02-24 01:03:00
//写文件;
public void writeTxtFile(string path,string fgf,ArrayList a)
{
//保存文件已存在
if (File.Exists(path))
{
Console.WriteLine("文件已存在!是否覆盖(O)、追加(A)该文件?退出直接按回车键。");
string over_flag = Console.ReadLine().ToString().Trim();
//追加到文件末尾
if (over_flag.Equals("A")||over_flag.Equals("a"))
{
try
{
StreamWriter sw = new StreamWriter(path,true,Encoding.GetEncoding("gb2312"));
IEnumerator cIE = a.GetEnumerator();
while (cIE.MoveNext())
{
ArrayList row =(ArrayList)cIE.Current;
//读取文本行中的字符段,并输出文件
IEnumerator rIE = row.GetEnumerator();
int length = row.Count;
int i = 0;
while (rIE.MoveNext())
{
string st = rIE.Current.ToString();
if (i==length-1)
sw.Write(st);
else
sw.Write(st+fgf);
i++;
}
sw.WriteLine();
}
sw.Close();
Console.WriteLine("写文件完毕,按回车键退出。。。");
Console.ReadLine();
}
catch(IOException ex)
{
Console.WriteLine(ex.ToString());
Console.WriteLine("写文件出错!请检查文件是否正确。按回车键退出。。。");
Console.ReadLine();
}
}
}
}
}
}
//文件太长,只保留了追加文件这个功能;
不知道这个论坛可不可以上传附件?那样可以把源程序附带上来;
6 楼
supercrsky [专家分:580] 发布于 2007-02-24 19:09:00
可以上传附件的
我来回复