[英]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: 我的选择是:
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字符串匹配算法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.