2011-06-28 77 views
3

我有一个应用程序从文本文件中读取信息,然后对它们进行分类并将它们放到数据库中。对于一个类别,我需要检查当前行后面的行并查找某个关键字?只读一次文件的下一行

我该如何阅读这一行?这应该发生在streamreader有当前行已经打开....

我在VS2010上使用c#。

编辑

所有下面的代码是在循环

string line = sReader.ReadLine(); //Note: this is used way above and lots of things are done before we come to this loop 

for (int i = 0; i < filter_length; i++) 
{ 
     if (searchpattern_queries[i].IsMatch(line) == true) 
     { 
       logmessagtype = selected_queries[i]; 

       //*Here i need to add a if condition to check if the type is "RESTARTS" and i need to get the next line to do more classification. I need to get that line only to classify the current one. So, I'd want it to be open independently * 

       hit = 1; 
       if (logmessagtype == "AL-UNDEF") 
       { 
        string alid = AlarmID_Search(line); 
        string query = "SELECT Severity from Alarms WHERE ALID like '" +alid +"'"; 
        OleDbCommand cmdo = new OleDbCommand(query, conn); 
        OleDbDataReader reader; 
        reader = cmdo.ExecuteReader(); 
        while (reader.Read()) 
        { 
         if (reader.GetString(0).ToString() == null) 
         { } 
         else 
         { 
          string severity = reader.GetString(0).ToString(); 
          if (severity == "1") 
           //Keeps going on..... 

此外,被打开.log文件可能会去(sReader.EndOfStream!)高达50 Mb类型...!这就是为什么我不喜欢阅读所有的线和保持跟踪!

+0

StreamReader没有“打开线路”。 –

+0

你不能只是调用'StreamReader.ReadLine()'? – rossipedia

+0

@Bryan:那会不会只是搬到那条线?我需要这条线再次读取下一个迭代.. – techmanc

回答

4

只需使用

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

和处理与for (int i = 0; i < lines.Length; i ++)环文件。

对于大文件,只需缓存“上一行”或执行带外ReadLine()即可。

+0

你如何做一个带外readline? – techmanc

1

您能不能再次致电reader.ReadLine()?或者是您需要在循环的下一次迭代中使用该行的问题?

如果这是一个相当小的文件,你有没有考虑使用File.ReadAllLines()读取整个文件?这可能会让它变得更简单,尽管在其他方面显然不那么干净了,而且对于大型文件来说更需要内存。

编辑:下面是一些代码来替代:

using (TextReader reader = File.OpenText(filename)) 
{ 
    string line = null; // Need to read to start with 

    while (true) 
    { 
     if (line == null) 
     { 
      line = reader.ReadLine(); 
      // Check for end of file... 
      if (line == null) 
      { 
       break; 
      } 
     } 
     if (line.Contains("Magic category")) 
     { 
      string lastLine = line; 
      line = reader.ReadLine(); // Won't read again next iteration 
     } 
     else 
     { 
      // Process line as normal... 
      line = null; // Need to read again next time 
     } 
    } 
} 
+0

我需要下一个迭代线。我只是需要它作为分类当前行的参考..有什么办法可以打开与当前readline隔离的行...我想过读取所有行......但有些情况下文件很大。 ..它不会是理想的! – techmanc

+0

@techmanc:究竟有多“巨大”?你可以随时保留你阅读的一行文本 - 棘手的一点是确保你使用它而不是阅读下一行。 –

+0

我读了100个文件,并将它们放入数据库中进行一次迭代。有些文件可能是50-100 MB的! – techmanc

0

在调用ReadLine之后,您可以保存流的位置,然后回到那个位置。然而这是非常低效的。

我会将ReadLine的结果存储到“缓冲区”中,并在可能时使用该缓冲区作为源。当它为空时,使用ReadLine。

0

我不是一个真正的文件IO专家......但为什么不能做这样的事情:

在您开始阅读线声明两个变量。

string currentLine = string.Empty 
string previousLine = string.Empty 

然后当你正在阅读...

previousLine = currentLine; 
currentLine = reader.ReadLine(); 
5

这里是一个过程,同时有你的下一行已经可以在当前行的成语:

public void ProcessFile(string filename) 
{ 
    string line = null; 
    string nextLine = null; 
    using (StreamReader reader = new StreamReader(filename)) 
    { 
     line = reader.ReadLine(); 
     nextLine = reader.ReadLine(); 
     while (line != null) 
     { 
      // Process line (possibly using nextLine). 

      line = nextLine; 
      nextLine = reader.ReadLine(); 
     } 
    } 
} 

这基本上是一个队列其中最多两个项目,或“一行预读”。

编辑:简化。