-1

次のようなデータを含むテキストファイルがあります入力データファイルタイプ:INdia.Txt

INdia(s) - Input Data File Exists .....

**------------------------------------------**
Feed Counts:
**------------------------------------------**
Records in the Input File            : 04686
Records Inserted in  Table : 04069
Records Inserted in  Table    : 00617
**-------------------------------------------**

出力ファイルでこのデータのみを取得する必要があります

Records in the Input File  : 04686
Records Inserted in  Table : 04069
Records Inserted in  Table    : 00617 

私が使用しているコード

try
        {
            int NumberOfLines = 15;
            string[] ListLines = new string[NumberOfLines];
            using (FileStream fs = new FileStream(@"d:\Tesco\NGC\UtilityLogs\Store.log", FileMode.Open))
            {
                using (StreamReader reader = new StreamReader(fs, Encoding.UTF8))
                {
                    string line = null;
                    while ((line = reader.ReadLine()) != null)
                    {
                        Console.WriteLine(line);

                        if (line.Contains("Records"))
                        {
                            //Read the number of lines and put them in the array
                            for (int i = 8; i < NumberOfLines; i++)
                            {
                                ListLines[i] = reader.ReadLine();
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
4

4 に答える 4

2

このLINQクエリはIEnumerable<string>、「Records」文字列で始まるファイルのすべての行を含むものを提供します。

var lines = File.ReadAllLines(path).Where(line => line.StartsWith("Records"));
于 2012-12-13T12:44:32.633 に答える
1

これを試して

   while ((line = reader.ReadLine()) != null)
   {
      if(!line.Contains("Records"))  //if line does not contain "Records"
      {
         continue;  //skip to next line/iteration
      }

      //else
      //process the line  
   }

行数がわかっている場合、これは機能する可能性があります

   int line_number = 1;
   int startline = 15;
   int endline = 100;
   while ((line = reader.ReadLine()) != null)
   {
      if(line_number >= startline && line_number <= endline)  
      {
         //process the line  
      }

      line_number++;
   }
于 2012-12-13T12:28:29.387 に答える
0

ファイルの内容が固定されている場合は、INDEXを使用してください。

StreamReaderを使用してファイルを読み取り、StreamReaderデータをNEWLINE charで分割すると、配列が取得され、その配列のインデックスが作成されます。

于 2012-12-13T12:29:12.433 に答える
0

ファイルの行数が常に同じである場合は、次を使用します。

 string[] lines = File.ReadAllLines(fileName);

 string line1 = lines[5];
 string line2 = lines[6];
 string line3 = lines[6];
 ...

またはこのようなものでも:

string[] lines = File.ReadAllLines(fileName);
string[] result = new string[3]; // No matter how many, but fixed

Array.Copy(lines, 5, result, result.Length);

ヘッダーが常に5行で、ファイルが常に1行で終わる場合は、動的な行数を設定することもできます。

string[] lines = File.ReadAllLines(fileName);
string[] result = new string[lines.Length - 6];
Array.Copy(lines, 5, result, result.Length);
于 2012-12-13T12:41:12.410 に答える