跳转文件并读取行的最有效方法?
我想使用 FileStream 并从文件开头开始查找,同时在文件中一次向前移动文件大小的 0.01%。
所以我想寻找文件中的一个位置,读取整行,如果它符合我的标准,我就完成了。 如果没有,我会寻找另一个 .01。
C# 可以,但首选 VB.NET。
我曾经在VB6中做过类似的事情...
FileOpen(1, CurrentFullPath, OpenMode.Input, OpenAccess.Read, OpenShare.Shared)
Dim FileLength As Long = LOF(1)
For x As Single = 0.99 To 0 Step -0.01
Seek(1, CInt(FileLength * x))
Dim S As String = LineInput(1)
S = LineInput(1)
filePosition = Seek(1)
If filePosition < 50000 Then
filePosition = 1
Exit For
End If
V = Split(S, ",")
Dim MessageTime As Date = CDate(V(3) & " " & Mid$(V(4), 1, 8))
Dim Diff As Integer = DateDiff(DateInterval.Minute, MessageTime, CDate(RequestedStartTime))
If Diff >= 2 Then
Exit For
End If
Next
但我不想使用FileOpen,我想使用FileStream。
任何帮助是极大的赞赏!
I want to use a FileStream and seek from the beginning of the file while moving forward in the file .01% of the file size at a time.
So I want to seek to a position in the file, read the entire line, if it matches my criteria I am done. If not, I seek ahead another .01.
C# is OK but VB.NET preferred.
I used to do it something like this in VB6...
FileOpen(1, CurrentFullPath, OpenMode.Input, OpenAccess.Read, OpenShare.Shared)
Dim FileLength As Long = LOF(1)
For x As Single = 0.99 To 0 Step -0.01
Seek(1, CInt(FileLength * x))
Dim S As String = LineInput(1)
S = LineInput(1)
filePosition = Seek(1)
If filePosition < 50000 Then
filePosition = 1
Exit For
End If
V = Split(S, ",")
Dim MessageTime As Date = CDate(V(3) & " " & Mid$(V(4), 1, 8))
Dim Diff As Integer = DateDiff(DateInterval.Minute, MessageTime, CDate(RequestedStartTime))
If Diff >= 2 Then
Exit For
End If
Next
But I don't want to use FileOpen, I want to use a FileStream.
Any help is greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我通常更喜欢 vb.net,但 C# 的迭代器块慢慢地赢得了我的青睐:
将其放入类库项目中并从 vb 中使用它,如下所示:(
我对您的标准采取了一些自由(假设固定宽度值而不是分隔值)使示例更容易)
I normally prefer vb.net, but C#'s iterator blocks are slowly winning me over:
Put it in a class library project and use it from vb like this:
(I took some liberties with you criteria (assumed fixed-width values rather than delimited) to make the example easier)
MSDN 上有一个 示例。
编辑回应评论:
我必须承认我有点困惑,因为您似乎坚持使用缓冲的 FileStream,但想一次读取一行文件? 您可以使用 StreamReader 非常简单地做到这一点。 我不知道 VB,但在 C# 中它会是这样的:
请参阅 http://msdn.microsoft.com/en-us/library/system.io.file.aspx。
There's an example on MSDN.
Edit in response to comment:
I must admit I'm a bit confused, as you seemed insistant on using a buffered FileStream, but want to read a file a line at a time? You can do that quite simply using a StreamReader. I don't know VB, but in C# it would be something like this:
See http://msdn.microsoft.com/en-us/library/system.io.file.aspx.
像这样的东西怎么样(C#版本):
编辑:VB版本在这里(警告-来自C#开发人员!)
what bout something like this (C# version):
EDIT: VB version here (warning - from a C# dev!)
这或多或少是代码的直接转换,我们使用 FileStream.Position 来指定要读取文件中的位置:
This is a more or less direct conversion of your code, where we use FileStream.Position to specify where in the file to read: