我有一个应用程序,它从文本文件中读取信息,然后对它们进行分类并将它们放入数据库中。对于一个类别,我需要检查当前行之后的行并查找某个关键字?
我如何阅读这一行?当流阅读器当前行已打开时,应该会发生这种情况......
我在 VS2010 上使用 C#。
Edit:
下面的所有代码都在一个while (!Reader.EndOfStream) loop
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 文件可能高达 50 Mb 类型...!这就是为什么我不太喜欢阅读所有行并跟踪!
这是一个在下一行已经可用的情况下处理当前行的习惯用法:
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();
}
}
}
这基本上是一个queue其中最多包含两项,或“预读一行”。
Edit:简化了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)