跳转文件并读取行的最有效方法?

发布于 2024-07-29 23:07:45 字数 1026 浏览 4 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回忆那么伤 2024-08-05 23:07:49

我通常更喜欢 vb.net,但 C# 的迭代器块慢慢地赢得了我的青睐:

public static IEnumerable<string> SkimFile(string FileName)
{
    long delta = new FileInfo(FileName).Length / 100;
    long position = 0;

    using (StreamReader sr = new StreamReader(FileName))
    {
        while (position < 100)
        {
            sr.BaseStream.Seek(position * delta, SeekOrigin.Begin);
            yield return sr.ReadLine();
            position++;
        }
    }
}

将其放入类库项目中并从 vb 中使用它,如下所示:(

Dim isMatch as Boolean = False
For Each s As String in SkimFile("FileName.txt")
    If (RequestedDate - CDate(s.SubString(3,11))).Minutes > 2 Then
       isMatch = True
       Exit For
    End If
Next s

我对您的标准采取了一些自由(假设固定宽度值而不是分隔值)使示例更容易)

I normally prefer vb.net, but C#'s iterator blocks are slowly winning me over:

public static IEnumerable<string> SkimFile(string FileName)
{
    long delta = new FileInfo(FileName).Length / 100;
    long position = 0;

    using (StreamReader sr = new StreamReader(FileName))
    {
        while (position < 100)
        {
            sr.BaseStream.Seek(position * delta, SeekOrigin.Begin);
            yield return sr.ReadLine();
            position++;
        }
    }
}

Put it in a class library project and use it from vb like this:

Dim isMatch as Boolean = False
For Each s As String in SkimFile("FileName.txt")
    If (RequestedDate - CDate(s.SubString(3,11))).Minutes > 2 Then
       isMatch = True
       Exit For
    End If
Next s

(I took some liberties with you criteria (assumed fixed-width values rather than delimited) to make the example easier)

独木成林 2024-08-05 23:07:49

MSDN 上有一个 示例

编辑回应评论:

我必须承认我有点困惑,因为您似乎坚持使用缓冲的 FileStream,但想一次读取一行文件? 您可以使用 StreamReader 非常简单地做到这一点。 我不知道 VB,但在 C# 中它会是这样的:

using (StreamReader sr = File.OpenText(pathToFile)) 
{
    string line = String.Empty;
    while ((line = sr.ReadLine()) != null) 
    {
        // process line
    }
}

请参阅 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:

using (StreamReader sr = File.OpenText(pathToFile)) 
{
    string line = String.Empty;
    while ((line = sr.ReadLine()) != null) 
    {
        // process line
    }
}

See http://msdn.microsoft.com/en-us/library/system.io.file.aspx.

动听の歌 2024-08-05 23:07:48

像这样的东西怎么样(C#版本):

using (var file = System.IO.File.OpenText(filename))
{
     while (!file.EndOfStream)
     {
          string line = file.ReadLine();
          //do your logic here
          //Logical test - if true, then break
     }
}

编辑:VB版本在这里(警告-来自C#开发人员!)

Using file as FileStream = File.OpenText(filename)
    while Not file.EndOfStream
         Dim line as string = file.ReadLine()
     ''//Test to break
     ''//exit while if condition met
    End While
End Using

what bout something like this (C# version):

using (var file = System.IO.File.OpenText(filename))
{
     while (!file.EndOfStream)
     {
          string line = file.ReadLine();
          //do your logic here
          //Logical test - if true, then break
     }
}

EDIT: VB version here (warning - from a C# dev!)

Using file as FileStream = File.OpenText(filename)
    while Not file.EndOfStream
         Dim line as string = file.ReadLine()
     ''//Test to break
     ''//exit while if condition met
    End While
End Using
柠北森屋 2024-08-05 23:07:47

这或多或少是代码的直接转换,我们使用 FileStream.Position 来指定要读取文件中的位置:

Using streamReader As System.IO.StreamReader = System.IO.File.OpenText(CurrentFullPath)
  For x As Single = 0.99 To 0 Step -0.01
    streamReader.BaseStream.Position = CLng(streamReader.BaseStream.Length * x)
    Dim S As String = streamReader.ReadLine()
    '... etc.
  Next
End Using

This is a more or less direct conversion of your code, where we use FileStream.Position to specify where in the file to read:

Using streamReader As System.IO.StreamReader = System.IO.File.OpenText(CurrentFullPath)
  For x As Single = 0.99 To 0 Step -0.01
    streamReader.BaseStream.Position = CLng(streamReader.BaseStream.Length * x)
    Dim S As String = streamReader.ReadLine()
    '... etc.
  Next
End Using
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文