2009-07-28 67 views
2

我想使用FileStream并从文件的开头进行查找,同时向前移动文件大小.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。

任何帮助,非常感谢!

+0

你的问题中提到想要在读取只有1%的文件一段时间,但它也说你想逐行阅读,所以我以这种方式回答了它。 – 2009-07-28 21:36:10

回答

3

这是你的代码,在这里我们使用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 
1

什么布特这样的事情(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 
+0

感谢乔希,但文件是大的方式逐行通过它。我需要能够在文件中找到一个位置。 – Doug 2009-07-28 23:06:21

0

我通常喜欢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像THI使用S:

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 

(我花了一些调戏你的标准(假定固定宽度的值,而不是分隔),以使示例更容易)

+0

乔尔,我喜欢这个,但我应该指定我需要使用.NET 2.0。你的代码是2.0吗? – Doug 2009-07-28 23:07:13

+0

不,你需要3.5的lamdba函数和.Any()扩展名。但C#部分全部是2.0。给5分钟,我会更新vb以使其兼容2.0。 – 2009-07-28 23:57:07

+0

另外,我应该警告你:该代码直接输入到回复窗口中,并且完全未经测试。 – 2009-07-29 00:01:30

0

有一个example on MSDN

编辑回应评论:

我必须承认我有点困惑,因为你好像在使用缓冲的FileStream insistant,但想读取文件在一个时间线?你可以简单地使用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

相关问题