如何在c#中查找和存储文件中的特定行

本文关键字:存储文件 查找 | 更新日期: 2023-09-27 18:13:37

我想搜索一些包含某些特定数据的行,并将这些数据存储在列表中,我该怎么做?我已经为我的项目编写了这段代码,但是这段代码只能找到并存储包含我想要的数据的第一行,因为所有这些行都以相同的结构开头,例如,我的数据在100、250、400、660行中重复出现,并且所有这些行都用"|PROD |OIL |"来表示。

double[] oil_prdc   = new double[10];
double[] water_prdc = new double[10];                  
double[] gas_prdc   = new double[10];
double[] water_injc = new double[10];
double[] gas_injc   = new double[10];  
int length_time = 5;
string[][] characters = new string[2391][];
string [] Charss;
string[][]counter = new string[20][];
while ((line = file.ReadLine()) != null)
{                    
    for (int i = 0; i < length_time; i++)
    {
        total_production.Add(null);
        total_production[i] = new List<_5_points>(); 
        while (line.Contains("|PROD    |OIL     |"))
        {
            Charss = line.Split('|');
            for (int j = 0; j < Charss.Length; j++)
            {
                if (j == 9)
                {
                    oil_prdc[i] = 1000 * Convert.ToDouble(Charss[j]);                                        
                }
                else if (j == 10)
                {
                    water_prdc[i] = Convert.ToDouble(Charss[j]);
                }
                else if (j == 11)
                {
                    gas_prdc[i] = 1000 * Convert.ToDouble(Charss[j]);
                }
            }
            count++;
            if (count > Charss.Length) break;
        }
        while (line.Contains(" |WINJ    |WAT     "))
        {
            Charss = line.Split('|');
            for (int jj = 0; jj < Charss.Length; jj++)
            {
                if (jj == 8)
                {
                    water_injc[i] = 1000 * Convert.ToDouble(Charss[jj]);
                }
            }
            count++;
            if (count > Charss.Length) break;
        }
        while (line.Contains(" |GINJ    |Passive "))
        {
            Charss = line.Split('|');
            for (int ij = 0; ij < Charss.Length; ij++)
            {
                if (ij == 9)
                {
                    gas_injc[i] = 1000 * Convert.ToDouble(Charss[ij]);
                }
            }
            count++;
            if (count > Charss.Length) break;
        }
        _5_points temp=new _5_points{OIL_PRD =oil_prdc[i],GAS_PRD=gas_prdc[i],WATER_PRD=water_prdc[i],WATER_INJ=water_injc[i],GAS_INJ=gas_injc[i]};
        total_production[i].Add(temp);
    }
}

如何在c#中查找和存储文件中的特定行

读取文件的示例:

try 
{
    // Create an instance of StreamReader to read from a file.
    // The using statement also closes the StreamReader.
    using (StreamReader sr = new StreamReader("TestFile.txt")) 
    {
        string line;
        // Read and display lines from the file until the end of 
        // the file is reached.
        while ((line = sr.ReadLine()) != null) 
        {
            Console.WriteLine(line);
        }
    }
}
catch (Exception e) 
{
    // Let the user know what went wrong.
    Console.WriteLine("The file could not be read:");
    Console.WriteLine(e.Message);
}

在每个"行"中搜索特定文本:

public static string getBetween(string strSource, string strStart, string strEnd)
{
    int Start, End;
    if (strSource.Contains(strStart) && strSource.Contains(strEnd))
    {
        Start = strSource.IndexOf(strStart, 0) + strStart.Length;
        End = strSource.IndexOf(strEnd, Start);
        return strSource.Substring(Start, End - Start);
    }
    else
    {
        return "";
    }
}

如何使用:

string text = "This is an example string and my data is here";
string data = getBetween(text, "my", "is");