如何从文件中读取数据? [英] How to read data from a file?

查看:139
本文介绍了如何从文件中读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,
我遇到一个问题,我只能读取一条记录.其他记录未显示.
文件中的数据为

Dear all,
I got a problem, that I can read only one record. The other record doesn''t show.
The data in file is

001 
Phanny 
F 
Toul Kork 
077 95 39 95 
123abc 
Monday, January 09, 2012 
Tuesday, January 31, 2012 
good 

002
Thary
M
PP
012345678
asf 
Tuesday, January 10, 2012 
Tuesday, January 10, 2012 
good


结果是
001 Phanny F Toul Kork 077 95 39 95 123abc 2012年1月9日,星期一,2012年1月31日,星期二.
此结果在ListView中显示如下,
ID名称性别地址电话护照SartDate EndDate Decriptiom
001 Phanny F PP 077953995 123abc Mon ... Tue ... good

这是我的代码
写入文件


And the reasult is
001 Phanny F Toul Kork 077 95 39 95 123abc Monday, January 09, 2012 Tuesday,January 31, 2012 good.
This result shows in ListView as below
ID Name Gender Address Phone Passport SartDate EndDate Decriptiom
001 Phanny F PP 077953995 123abc Mon... Tue... good

Here is my code
writing to a file

string tLine = "";
            StreamWriter objWriter = new StreamWriter("text.txt",true);
            string[] arrTest = new string[9];
            arrTest[0] = txtID.Text;
            arrTest[1] = txtName.Text;
            arrTest[2] = cmbGender.Text;
            arrTest[3] = txtAddress.Text;
            arrTest[4] = txtPhone.Text;
            arrTest[5] = txtPassport.Text;
            arrTest[6] = dateTPStartdate.Text;
            arrTest[7] = dateTPEndDate.Text;
            arrTest[8] = txtDescription.Text;
            //ind++;
            for (j = 0; j < 9; j++)
            {
                objWriter.WriteLine(arrTest[j]+" ");
            }
            objWriter.WriteLine();
            objWriter.Close();


从文件读取


Reading from a file

ListViewItem lis = new ListViewItem();

           FileInfo afile = new FileInfo("text.txt");
           if (afile.Exists)
           {
               StreamReader objReader = new StreamReader("text.txt", true);
               string textLine = "";
               do
               {
                   textLine = objReader.ReadLine();
                   lis.SubItems.Add(textLine);
               } while (objReader.Peek() != -1);

               listView1.Items.Add(lis);
               objReader.Close();
           }
           else {
               MessageBox.Show("There is no file");
           }



请帮帮我.
Thanks



Please help me.
Thanks

推荐答案

使用这种方法检查文件结尾.

Use this way to check end of file.

while ((textLine = objReader.ReadLine()) != null)
           {
                lis.SubItems.Add(textLine);
           }




您也可以尝试使用BinaryReader和BinaryWriter.对于结构化数据,它比StreamReader和StreamWriter更好.




Also you can try to use BinaryReader and BinaryWriter. It is better than StreamReader and StreamWriter for structural data.




如果对每个记录使用定界符,则将很容易读取记录.

请参阅 MSDN
Hi,

It will be easy to read record if you use delimiter for each record.

Please see MSDN


这篇关于如何从文件中读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆