如何在c#中解析文本文件?

时间:2017-01-13 05:03:16

标签: c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace readfrom_textfile {
    class Program {
        static void Main(string[] args) {
            var fileStream = new FileStream(@"D:\Practise\test.txt", FileMode.Open, FileAccess.Read);
            using(var streamReader = new StreamReader(fileStream, Encoding.UTF8)) {
                string line;
                while ((line = streamReader.ReadLine()) != null) {
                    string[] data = line.Split(new char[] {
                        '('
                    }, StringSplitOptions.RemoveEmptyEntries);
                    string bikeName = data[0].Trim().Split(' ').Last();
                    bikeName = bikeName.Remove(bikeName.Length - 1);
                    string[] usageData = data[1].Split(new char[] {
                        ';'
                    }, StringSplitOptions.RemoveEmptyEntries);
                    string issuedBike = usageData[0].Trim().Split(' ')[2];
                    string inUseBike = usageData[1].Trim().Split(' ')[2];
                    Console.WriteLine("Name:" + bikeName);
                    Console.WriteLine("Total:" + issuedBike);
                    Console.WriteLine("InUse:" + inUseBike);
                    Console.WriteLine();
                }

            }
            Console.ReadLine();
        }

在我的文本文件中,该行是:

  

雅马哈的用户:(共发布2辆自行车;共使用2辆自行车).newline           一号线....................东西[换行]           2号线..................东西[换行]           自行车细节           // EMPTYLINE           1125 Monica YamahaR15(v3.0)(汽油/ 7788 4903),星期三1/18 8:53           1128 Dhini Yamaha Fz(v2.0)(汽油/ 7748 4903),开始于Wed 1/18 9:53

    This is the line.I want to get this line from my file and split into < pre >
        Name: Yamaha;
    Total: 1;
    Inuse: 1;
    members: Monica[Wed 1 / 18 8: 53], dhini[Wed 1 / 18 9: 53] < /pre>
    Like this format.

1 个答案:

答案 0 :(得分:2)

如果您在txt文件的下一行中存储每条记录(每1条记录1行),例如:

Users of Yamaha: (Total of 1 bike issued; Total of 1 bike in use)
Users of Hero: (Total of 6 bike issued; Total of 2 bike in use)

然后您可以使用以下代码将输出存储在文件中。

public void Main(string[] args)
{
   var fileStream = new FileStream(@"D:\Practise\test.txt", FileMode.Open, FileAccess.Read);
   using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
   {
      string line;
      string createTextFile = "";
      string outputPath = @"D:\Practise\Output.txt";
      while ((line = streamReader.ReadLine()) != null)
      {
         string[] data = line.Split(new char[] { '(' }, StringSplitOptions.RemoveEmptyEntries);
         string bikeName = data[0].Trim().Split(' ').Last();
         bikeName = bikeName.Remove(bikeName.Length - 1);
         string[] usageData = data[1].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
         string issuedBike = usageData[0].Trim().Split(' ')[2];
         string inUseBike = usageData[1].Trim().Split(' ')[2];

         Console.WriteLine("Name:" + bikeName);
         Console.WriteLine("Total:" + issuedBike);
         Console.WriteLine("InUse:" + inUseBike);
         Console.WriteLine();

         createTextFile += "Name:" + bikeName + Environment.NewLine;
         createTextFile += "Total:" + issuedBike + Environment.NewLine;
         createTextFile += "InUse:" + inUseBike + Environment.NewLine + Environment.NewLine;
      }
      File.WriteAllText(outputPath, createTextFile);
   }
   Console.ReadLine();
}