简体   繁体   English

C#文本文件浏览和文件写入

[英]C# text file browse and file write

I have this very large text file (around 35 000+ lines of information) and I would like to extract certain lines from it and place them in another text file. 我有一个非常大的文本文件(大约35 000多行信息),我想从中提取某些行并将其放在另一个文本文件中。

The text file goes like this: 文本文件如下所示:

    Feature info
    Feature name: 123456
    Version: 1

    Tokens total: 35
    Tokens remaining: 10

And I'd like to extract Feature name and tokens total. 我想提取特征名称和令牌总数。 What I had in mind was a form with two buttons: 1 for browsing the file and the other to do the whole reading and writing to file part, of course in the same line by line format. 我想到的是一个带有两个按钮的表单:一个用于浏览文件,另一个用于完成对文件部分的整体读取和写入,当然是采用相同的逐行格式。

Anyone have any clues on how to do it? 有人对如何做有任何线索吗? I have searched and I haven't really found specific things, also quite new to file read/write... 我已经搜索过,但还没有真正找到具体的东西,对于文件读/写也很新...

EDIT 编辑

Ok here is what I have so far and it works: 好的,这是我到目前为止所拥有的,并且可以正常工作:

private void button1_Click(object sender, EventArgs e)
    {

        int counter = 0;
        string line;
        string s1 = "Feature name";
        string s2 = "Tokens total";


        // Read the file and display it line by line.
        System.IO.StreamReader file = new System.IO.StreamReader("d:\\license.txt");
        using (System.IO.StreamWriter file2 = new System.IO.StreamWriter(@"D:\test.txt")) 
         while ((line = file.ReadLine()) != null)
          {
              if (line.Contains(s1) || line.Contains(s2))
              {
                  file2.WriteLine(line);
                  counter++;
              }
          }

        file.Close();

And this is done by a single button. 这是通过一个按钮完成的。 What I want though is to be able to search for the file I want and then use another button in order to do all writing process 我想要的是能够搜索我想要的文件,然后使用另一个按钮来完成所有写入过程

You can use StreamReader and StreamWriter for read/write file 您可以使用StreamReaderStreamWriter读取/写入文件

To extract specific part of text, you may use Regex.Matches , it will return Match , then you can retrieve the defined group in Match.Groups 要提取文本的特定部分,你可以使用Regex.Matches ,它将返回匹配 ,那么你可以检索所定义的组Match.Groups

// Search name
Match mu = Regex.Match(line, @"Feature name: (\d+)");

// Get name
if (mu.Groups.Count == 1) Console.Writeline(mu.Groups[0].Value);

Answer for the EDIT : 编辑的答案:

You can store the read data in property or private field in the form class. 您可以将读取的数据存储在表单类的属性或私有字段中。 Use String or StringBuilder preferably. 最好使用String或StringBuilder。 When the second button is clicked check if there is stored data and write it to the output file. 单击第二个按钮时,检查是否存储了数据并将其写入输出文件。

private StringBuilder data = new StringBuilder();

private void button2_Click(object sender, EventArgs e)
{
    if(data.Length > 0)
    {
        using(System.IO.StreamWriter file2 = new System.IO.StreamWriter(@"D:\test.txt"))
        {
            file2.Write(data.ToString());
        }
    }
}

private void button1_Click(object sender, EventArgs e)
{
    // Clear the previous store data
    data.Clear();

    // ...

    System.IO.StreamReader file = new System.IO.StreamReader("d:\\license.txt"); 
    while ((line = file.ReadLine()) != null)
    {
        if (line.Contains(s1) || line.Contains(s2))
        {
            sb.AppendLine(line);
            counter++;
        }
    }

    file.Close();
}

Please add using for System.IO and surround your StreamReader and StreamWriter with using block so your code will be more readable and you won't forget to release the used resources. 请添加用于System.IO的using,并使用using块包围StreamReader和StreamWriter,这样您的代码将更具可读性,并且您将不会忘记释放已使用的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM