2014-09-24 133 views
1

我试图通过将联系人存储在文本文件中来制作联系簿。例如,假设我有两个名为first name和surname的字符串,并且在文本文件中我有一个名字,下一行是姓氏。这是目前我有的代码,但我不知道我在do循环中需要做什么,如何读取一行,将其插入字符串xxx,读取下一行并将其存储在字符串yyy中?将文本文件读入数组?

public Contacts[] getAllContacts() 
    { 

     List<Contacts> theContactList = new List<Contacts>(); 

     string file_name = "Contacts.txt"; 
     string textLine = ""; 

     if (System.IO.File.Exists(file_name) == true) 
     { 
      System.IO.StreamReader objReader; 
      objReader = new System.IO.StreamReader(file_name); 


      do 
      { 
       objReader.ReadLine() + "\r\n"; 

      } while (objReader.Peek() != 1); 

     } 
     if (theContactList.Count > 0) 
     { 
      return theContactList.ToArray(); 

     } 
     else 
     { 
      return null; 
     } 


    } 

我还需要能够存储多个联系人和更多的领域,如地址,电话号码等在文本文件中。

+0

你能解释一下“联系人”是数据类型的吗? (在代码示例中...很难在文本示例中阅读) – Thomas 2014-09-24 09:47:10

+0

@Thomas对不起,这是另一个包含字段的类,如公共字符串姓氏,公共字符串first_name – CodingNub 2014-09-24 09:50:10

+0

你看过http: //msdn.microsoft.com/en-us/library/ezwyzy7b.aspx? – 2014-09-24 09:52:31

回答

2

根据推定,总是有2行那里,它们具有相同的顺序将下面的代码可以工作有:

public Contacts[] getAllContacts() 
{ 

    List<Contacts> theContactList = new List<Contacts>(); 

    string file_name = "Contacts.txt"; 
    string textLine = ""; 
    bool firstNameLine = true; 

    if (System.IO.File.Exists(file_name) == true) 
    { 
     System.IO.StreamReader objReader; 
     objReader = new System.IO.StreamReader(file_name); 
     Contact newContact; 

     do 
     { 
      textLine = objReader.ReadLine(); 
      if (firstNameLine) 
      { 
       newContact = new Contact(); 
       newContact.firstName = textLine; 
      } 
      else 
      { 
       newContact.sureName = textLine; 
       theContactList.Add(newContact); 
      } 

      firstNameLine = !firstNameLine; 

     } while (objReader.Peek() != 1); 

    } 
    if (theContactList.Count > 0) 
    { 
     return theContactList.ToArray(); 

    } 
    else 
    { 
     return null; 
    } 


} 

因此,在这种情况下,代码usese一个布尔变量,它定义该行读是用于填充哪个字符串。如果它超过2行(超过2个字符串),那么将需要一个整型变量(它会增加+1,然后在读取最后一行时重置),而不是if语句。

0

使用这样的:

string [] file_array = File.ReadAllLines("path"); 

PS:using System.IO; 此功能读取整个文件,并存储其线阵列可以编辑阵列线,然后使用

File.WriteAllLines("path",file_array);

把它们放回文件中。

2

使用XML或CSV文件,甚至只是一个文本文件,但在同一行上的每个“联系人”并将其解析出来可能会更好。

下面的代码会做你想要的。

public Contacts[] GetAllContacts() 
    { 
     List<Contacts> contacts = new List<Contacts>(); 
     const string filePath = "Contacts.txt"; 

     if (File.Exists(filePath)) 
     { 
      using (StreamReader sr = new StreamReader(filePath)) 
      { 
       do 
       { 
        Contacts contact = new Contacts(); 
        contact.FirstName = sr.ReadLine(); 
        contact.Surname = sr.ReadLine(); 
        contacts.Add(contact); 

       } while (sr.Peek() != -1); 
      } 
     } 

     return contacts.ToArray(); 
    } 
+0

不得不说,这并不比我的想法复杂(与两条线总是存在的情况相同),因此+1 – Thomas 2014-09-24 13:04:14

1
while (objReader.Peek() != -1) 
{ 
    theContactList.Add(new Contact() { Firstname = objReader.ReadLine(), SurName = objReader.ReadLine()}); 
} 

使用初始化列表。 (显然在Contact类成员上做出了假设)

+0

{}在这个组合中做了什么? (新联系人后的{}} – Thomas 2014-09-24 13:05:57

+0

对象初始值设定项列表。(我以前忘了添加属性名称。) – ths 2014-09-24 13:22:04

+0

啊,这就是为什么它看起来很奇怪。 TNX。很酷的想法 – Thomas 2014-09-24 13:24:46