簡體   English   中英

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

[英]Ignore certain lines in a text file (C# Streamreader)

我正在嘗試一種從正在編寫的程序中刪除記錄的方法。 我有一個文本文件,其中所有客戶數據分布在一組行中,我一次讀入這些行並將它們存儲在一個列表中

編寫時,我只是附加到文件中。 但是,對於刪除,我想到了不再需要在行的開頭添加*或#等字符的想法。 但是我不確定如何做到這一點

以下是我當前讀取數據的方式:

提前致謝

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();

由於使用的是字符串數組,因此只需執行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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM