简体   繁体   English

哪些行未读? 流阅读器

[英]What lines have not been read? Streamreader

I'm developing a file reader for a complex format. 我正在开发一种复杂格式的文件阅读器。 There are literally hundreds of different entries. 实际上有数百种不同的条目。 In the way I'm doing it now I need to use two Streamreaders because I need to extract some information before. 按照现在的方式,我需要使用两个Streamreaders因为我之前需要提取一些信息。 These files are big enough to not read them at once. 这些文件足够大,无法一次读取它们。

What I want is to notify the user what lines have not been read. 我想要的是通知用户尚未读取的行。 My structure is like this: 我的结构是这样的:

Streamreader file1 = new Streamreader(path);
while((line=file1.Readline()) != null)
{
     if(line.StartsWith("HELLO")
{
//...
}
//... more conditions
}



Streamreader file2 = new Streamreader(path);
while((line=file2.Readline()) != null)
{
     if(line.StartsWith("GOOD MORNING")
{
//...
}
//...more conditions 
}

So if my reader was perfect at the end all lines are read. 因此,如果我的读者最终是完美的,则将阅读所有行。 As things can be bizarre some entries can be not yet implemented, and I want to catch that lines. 由于事情可能很奇怪,所以某些条目可能尚未实现,我想抓住这句话。 The problem here, as you see, is having two StreamReaders . 如您所见,这里的问题是有两个 StreamReaders

My options are: 我的选择是:

  1. Store in a array all not read lines and then use it for the second reading, subtracting line by line after reading it. 将所有未读取的行存储在数组中,然后将其用于第二次读取,读取后逐行减去。 Not good because I will be storing several thousand of lines there. 不好,因为我将在那里存储数千行。
  2. Add all conditions in the second StreamReader to the first (all added) so this way I will know what lines are going to be read the second time. 将第二个StreamReader中的所有条件添加到第一个(全部添加)中,这样我将知道第二次将读取哪些行。 Better than previous but I need to modify my code in several places to make it run properly. 比以前更好,但是我需要在几个地方修改代码以使其正常运行。 I mean, when I wanted to implement the reading a new entry (second StreamReader ) I will need to modify the first StreamReader too. 我的意思是,当我想实现读取新条目(第二个StreamReader )时,我也需要修改第一个StreamReader

Is there any suggestion or any better way of doing this? 有什么建议或更好的方法吗?

I would create some predicates function translating line, ie: 我将创建一些谓词函数转换线,即:

class PredicateResult{
    public En_LineType type;
    public String data;
}

private PredicateResult FirstReader(String line){
    if(line.StartsWith("HELLO")){
        return new PredicateResult{
            type = En_LineType.Hello,
            data = ...
        }
    }
}

That way, you have two functions which can be used to check if line matches to any of them. 这样,您就有两个函数可用于检查行是否与其中任何一个匹配。 Additionaly you can easly change condition on which you are matching line and you can support different formats. 另外,您可以轻松更改要匹配的行的条件,并且可以支持不同的格式。

There are many string searching algorithms. 有很多字符串搜索算法。 Most of them uses hashing with Windowing Algorithm , which you can get an idea from What is Sliding Window Algorithm? 他们中的大多数人都将“哈希算法”与“ 开窗算法 ”一起使用,您可以从“滑动窗算法”中了解到什么? Examples? 例子?

Each algorithm has slight differences in general complexity or in worst case scenarios etc. You could pick one which you decide that suits most into your application: 每种算法在一般复杂度或最坏情况等方面都有细微的差别。您可以选择一种最适合您的应用程序的算法:

Rabin–Karp algorithm Rabin–Karp算法

Aho–Corasick string matching algorithm Aho–Corasick字符串匹配算法

Knuth–Morris–Pratt algorithm Knuth–Morris–Pratt算法

Boyer–Moore string search algorithm Boyer–Moore字符串搜索算法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM