使用Stream Writer在文本文件中写入数据,但如何格式化

时间:2013-08-03 13:30:54

标签: c#

Names               Date               Time
Sandra              11/18/2013         10.12AM
Denise              12/21/2013         10.10PM
Prnikshenth         11/11/2019         12.00AM

using System;
using System.Collections;
using System.IO;
class FunWithScheduling
{
       public void AddView()
       {
                  FileStream s = new FieStream("Scheduler.txt",FileMode.Append,FileAccess.Write);
                  StreamWriter w = new StreamWriter(s);
                  Console.WriteLine("Enter the Name of the Person To Be Met:");
                  string name = Console.ReadLine();
                  w.Write(name);
                  w.Flush();
                  w.Close();
                  s.Close();
       }
       public static void Main(string[] args)
       {
           FunWithScheduling a = new FunWithScheduling();
           a.AddView();
       } 
}      

我使用此代码添加名称,但它会像这样存储

Names               Date               Time
Sandra              11/18/2013         10.12AM
Denise              12/21/2013         10.10PM
Prnikshenth         11/11/2019         12.00AMShawn

我添加了Shawn,但这就是它如何陷入困境。

2 个答案:

答案 0 :(得分:1)

您之前需要写一个新行。我还添加了using语句,因此您无需手动调用close

using (StreamWriter sw = File.AppendText(@"Scheduler.txt"))
{
   sw.Write(Environment.NewLine + name);
}

答案 1 :(得分:-1)

尝试以下可能有帮助的解决方案

string line = name + "\t" + DateTime.Now.Date.ToString() + "\t" + DateTime.Now.Time.ToString();

w.WriteLine(line );

您可能只需要分别检查日期和时间的格式。