忽略文本文件中的某些行(c# Streamreader)

本文关键字:Streamreader 文本 文件 | 更新日期: 2023-09-27 18:17:43

我正试图找出一种从我正在编写的程序中删除记录的方法。我有一个文本文件,其中所有的客户数据分布在一组行中,我每次读取一行并将它们存储在List

中。

写的时候,我只是把它附加到文件中。但是,对于删除,我想到在不再需要的行前面添加一个字符,如*或#。然而,我不确定如何做到这一点

下面是我当前如何读取 中的数据:

Thanks in advance

StreamReader dataIn = null;
        CustomerClass holdcus; //holdcus and holdacc are used as "holding pens" for the next customer/account
        Accounts holdacc;
        bool moreData = false;
        string[] cusdata = new string[13]; //holds customer data
        string[] accdata = new string[8]; //holds account data

        if (fileIntegCheck(inputDataFile, ref dataIn))
        {
            moreData = getCustomer(dataIn, cusdata);
            while (moreData == true)
            {
                holdcus = new CustomerClass(cusdata[0], cusdata[1], cusdata[2], cusdata[3], cusdata[4], cusdata[5], cusdata[6], cusdata[7], cusdata[8], cusdata[9], cusdata[10], cusdata[11], cusdata[12]);
                customers.Add(holdcus);
                int x = Convert.ToInt32(cusdata[12]);
                for (int i = 0; i < x; i++) //Takes the ID number for the last customer, as uses it to set the first value of the following accounts
                {                                                     //this is done as a key to which accounts map to which customers
                    moreData = getAccount(dataIn, accdata);
                    accdata[0] = cusdata[0];
                    holdacc = new Accounts(accdata[0], accdata[1], accdata[2], accdata[3], accdata[4], accdata[5], accdata[6], accdata[7]);
                    accounts.Add(holdacc);
                }

                    moreData = getCustomer(dataIn, cusdata);
            }
        }
        if (moreData != null) dataIn.Close();

忽略文本文件中的某些行(c# Streamreader)

由于您使用字符串数组,您可以只执行cusdata[index] =" #"+cusdata[index]将其附加到该行的开头。然而,如果你的问题是如何从文件中删除它,为什么不跳过上面的步骤,只是不添加你想要删除的行写文件时?

下面是一个小的读/写示例,应该可以满足您的需求。如果没有,请在评论中告诉我。

class Program
{
    static readonly string filePath = "c:''test.txt";
    static void Main(string[] args)
    {
        // Read your file
        List<string> lines = ReadLines();
        //Create your remove logic here ..
        lines = lines.Where(x => x.Contains("Julia Roberts") != true).ToList();
        // Rewrite the file
        WriteLines(lines);

    }
    static List<string> ReadLines()
    {
        List<string> lines = new List<string>();
        using (StreamReader sr = new StreamReader(new FileStream(filePath, FileMode.Open)))
        {
            while (!sr.EndOfStream)
            {
                string buffer = sr.ReadLine();
                lines.Add(buffer);
                // Just to show you the results
                Console.WriteLine(buffer); 
            }
        }
        return lines;
    }
    static void WriteLines(List<string> lines)
    {
        using (StreamWriter sw = new StreamWriter(new FileStream(filePath, FileMode.Create)))
        {
            foreach (var line in lines)
            {
                sw.WriteLine(line);
            }
        }
    }
}

对于这个

,我使用了下面的"数据样本"
Matt Damon 100 222
Julia Roberts 125 152
Robert Downey Jr. 150 402
Tom Hanks 55 932