在文本文档中写入数据 [英] write data in a text document

查看:77
本文介绍了在文本文档中写入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨每一个

我在C#中编写一个程序,通过使用正则表达式类,我在一个大文本文档中搜索指定的模式。之后我想把一个匹配的匹配写入一个文本文件。我很困惑使用file.writealltext或file.writealllines或其他方法。请帮助我



最好的问候



Davood choopani

hi every one
I am writing a program in C# that by using regular expression classes I search a big text document for specified pattern. after that I want to write finded matches to a text file.I am confused in using file.writealltext or file.writealllines or other methods.please help me

best regards

Davood choopani

推荐答案

来自MSDN的示例http://msdn.microsoft.com/library/vstudio/8bh11f1k.aspx [ ^ ]:



这些示例显示了各种写入方式文本到文件。前两个示例使用System.IO.File类上的静态方法将完整的字符串数组或完整的字符串写入文本文件。示例#3显示了在写入文件之前必须单独处理每一行时如何向文件添加文本。示例1-3全部覆盖文件中的所有现有内容。示例#4显示了如何将文本附加到现有文件。



An example from MSDN http://msdn.microsoft.com/library/vstudio/8bh11f1k.aspx[^]:

These examples show various ways to write text to a file. The first two examples use static methods on the System.IO.File class to write either a complete array of strings or a complete string to a text file. Example #3 shows how to add text to a file when you have to process each line individually before writing to the file. Examples 1-3 all overwrite all existing content in the file. Example #4 shows how to append text to an existing file.

class WriteTextFile
    {
        static void Main()
        {

            // These examples assume a "C:\Users\Public\TestFolder" folder on your machine.
            // You can modify the path if necessary.

            // Example #1: Write an array of strings to a file.
            // Create a string array that consists of three lines.
            string[] lines = {"First line", "Second line", "Third line"};
            System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);


            // Example #2: Write one string to a text file.
            string text = "A class is the most powerful data type in C#. Like structures, " +
                           "a class defines the data and behavior of the data type. ";
            System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);

            // Example #3: Write only some strings in an array to a file.
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"))
            {
                foreach (string line in lines)
                {
                    if (line.Contains("Second") == false)
                    {
                        file.WriteLine(line);
                    }
                }
            }

            // Example #4: Append new text to an existing file
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt", true))
            {
                file.WriteLine("Fourth line");
            }  
        }
    }
    /* Output (to WriteLines.txt):
        First line
        Second line
        Third line

     Output (to WriteText.txt):
        A class is the most powerful data type in C#. Like structures, a class defines the data and behavior of the data type.

     Output to WriteLines2.txt after Example #3:
        First line
        Third line

     Output to WriteLines2.txt after Example #4:
        First line
        Third line
        Fourth line
     */


这篇关于在文本文档中写入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆