2011-10-06 39 views
3

想知道如果你有更好的建議,我已經想出了。寫入文件和格式化行。需要的結果

我有要求以特定格式編寫文本文件 每個字段必須從所述位置開始,並且必須用空格填充,直到下一個字段。

例子:

 Field   Position in the row 
     Name    1 
     Surname   20 
     Address   50 
     Country   90 
     DOB    120 
     MaritalStatus 160 

下面是我的原型的嘗試,有沒有這樣做的一個整潔的更好的辦法? 需要在單元測試中測試行中的位置是否正確?

有什麼建議嗎?

   class Program 
       { 
        static void Main(string[] args) 
        { 
         Customer customer=new Customer(); 
         customer.Name = "Jo"; 
         customer.Surname = "Bloggs"; 
         customer.Address = " 1 NewYork Road"; 
         customer.Country = "UK"; 
         customer.DOB = "29/04/1990"; 
         customer.MaritalStatus = "Married"; 

         StringBuilder sb=new StringBuilder(); 
         CreateHeader(customer,sb); 
         sb.AppendLine(""); 
         CreateRow(customer, sb); 
         sb.AppendLine(""); 

         IOExtensions.WriteToFile(sb.ToString(), "TestFile.txt"); 

        } 

        private static void CreateHeader(Customer customer,StringBuilder sb) 
        { 
         /* 
          * 
          Field   Position in the row 
          Name    1  
          Surname   20 
          Address   50 
          Country   90 
          DOB    120 
          MaritalStatus 160 

          */ 
         //First Field 
         sb.Append(FormatValue("Name", 19)); 
         sb.Append(FormatValue("Surname", 29)); 
         sb.Append(FormatValue("Address", 39)); 
         sb.Append(FormatValue("Country", 29)); 
         sb.Append(FormatValue("DOB", 39)); 

         //Last field does not matter 
         sb.Append(FormatValue("MaritalStatus", 9)); 


        } 
        private static void CreateRow(Customer customer, StringBuilder sb) 
        { 
         /* 
          * 
          Field   Position in the row 
          Name   1 
          Surname   20 
          Address   50 
          Country   90 
          DOB    120 
          MaritalStatus 160 

          */ 
         //First Field 
         sb.Append(FormatValue(customer.Name, 19)); 
         sb.Append(FormatValue(customer.Surname, 29)); 
         sb.Append(FormatValue(customer.Address, 39)); 
         sb.Append(FormatValue(customer.Country, 29)); 
         sb.Append(FormatValue(customer.DOB, 39)); 

         //Last field does not matter 
         sb.Append(FormatValue(customer.MaritalStatus, 19)); 


        } 

        private static string FormatValue(string value, int maxLength) 
        { 
         //TODO ADD OTHER STUFF HERE 
         return value.PadRight(maxLength, ' '); 
        } 
       } 

       public static class IOExtensions 
       { 
        public static void WriteToFile(string text, string path) 
        { 
         using (var fs = File.CreateText(path)) 
         { 
          fs.Write(text); 
         } 
        } 
       } 
       public class Customer 
       { 
        public string Name { get; set; } 
        public string Surname { get; set; } 
        public string Address { get; set; } 
        public string Country { get; set; } 
        public string DOB { get; set; } 
        public string MaritalStatus { get; set; } 
       } 
      } 
+2

這種問題可能更適合http://codereview.stackexchange.com – StriplingWarrior

+1

你可以使用FileHelpers http://www.filehelpers.com/default.html – kd7

回答

2

我一直這樣做,這是我的建議....帶上你的課並重寫「ToString」函數或創建一個名爲別的自定義函數。使用「PadRight」字符串函數可以使用正確的填充來創建固定長度的字段。

例(你需要添加字段到的ToString的其餘部分):

  public class Customer 
      { 
       public string Name { get; set; } 
       public string Surname { get; set; } 
       public string Address { get; set; } 
       public string Country { get; set; } 
       public string DOB { get; set; } 
       public string MaritalStatus { get; set; } 

       public override string ToString() 
       { 
        return String.Format("{0}{1}{2}", 
         Name.PadRight(20), 
         Surname.PadRight(30), 
         Address.PadRight(40) 
         ); 
       } 
      } 

注:我們正在使用的長度來控制每個字段的起始位置。如果第一個字段是20個字符,那麼下一個字段將從21開始,並達到您定義的長度。

現在,遍歷數據並填充對象並調用customer.ToString()寫出格式化的字符串。

+0

。就像你的建議,我打算重寫字符串,但不知道如何在需要的位置將字符串連接到一起 – user9969

+0

PadRight()使它成爲一個固定寬度的字段,所以你只需將每個字段的長度和下一個字段將在下一個位置開始。例如,姓名= 20字符和姓氏= 30,那麼你做Name.PadRight(20),SurnamePadRight(30)。 – Zachary

0

建議:

  • 客戶對象未在使用「CreateHeader()」方法 - 將其刪除
  • 考慮重構的將writeToFile()來處理的情況下,你會發現自己通過迭代Customer對象的列表或數組。將分開寫每個客戶對象,而不是通過一個大字符串
+0

謝謝你的建議。只需要找到一個方法來寫一個字符串到一個特定的位置,如問題所述。任何建議 – user9969

+0

@ user231465我贊成Zachary的回答。您可能會發現它符合您的需求。 – Arun

0

它看起來像FileHelpers圖書館會很好地做的伎倆。我已經將它用於CSV文件,但它似乎也處理固定寬度的文件。

+0

嗨,不能使用第三方庫 – user9969

1

如果您知道每列的寬度,您可以使用String.Format來格式化該行。例如:

string s = String.Format("{0,-19}{1,-29}", customer.Name, customer.Surname); 
sb.AppendLine(string); 

當然,您的線條會稍長些。但是你可以在一個電話中完成所有事情。

負對齊值給出左對齊。正值可以在現場正確對齊。您可以使用StringBuilder.AppendFormat

請參閱Composite Formatting瞭解有關使用這兩種方法之一進行格式設置的更多信息。

這裏唯一的缺點是如果你想放在字段中的值比字段寬度長。如文檔所述:

如果對齊值小於格式化字符串的長度,則忽略對齊,並將格式化字符串的長度用作字段寬度。

+0

。我想知道是否有一種方法來寫只有例如「20」和該字符串寫在位置20.而不是我的價值觀。我有很長的名單,每列的定位。讓我明白這個「{0,-19} {1,-29}」,將從30等開始在「0-19」秒之間寫第一個字符串。問題是我將不得不計算很多。 – user9969

+0

@ user231465:這些值是字段的寬度,而不是起始位置。所以在你的例子中,前兩個實際上是'{0,-20} {1,-30}'。是的,你必須從偏移量計算字段寬度。這不應該證明困難。 –