我有一个包含这样数据的文本文件 输入数据文件类型: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;
}
对此的任何帮助都会很棒
由于 王子
答案 0 :(得分:2)
此LINQ查询将为您提供IEnumerable<string>
,其中包含来自file的所有行,以“Records”字符串开头:
var lines = File.ReadAllLines(path).Where(line => line.StartsWith("Records"));
答案 1 :(得分: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++;
}
答案 2 :(得分:0)
如果您的文件是固定内容,请使用INDEX。
使用StreamReader读取文件,然后通过NEWLINE char拆分StreamReader数据,您将获得一个数组,然后为该数组添加索引。
答案 3 :(得分: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行,文件总是以一行结束,你甚至可以拥有动态行数:
string[] lines = File.ReadAllLines(fileName);
string[] result = new string[lines.Length - 6];
Array.Copy(lines, 5, result, result.Length);