TextStream ReadPreviousLine [英] TextStream ReadPreviousLine
问题描述
你好.
有人可以帮助我阅读上一行吗?
Could somebody help me with reading previous line?
在发布的示例中,我使用StreamReader. ReadNextLine可以.但是用于读取前一行仅在文件开始时可用.如果我深陷文件中,则必须等待10分钟.对于上一行.
In posted example I use StreamReader. It is OK for ReadNextLine. but for reading previous line is usable only on start of file. If I'm deep in the file I have to waiting 10 min. for previous line.
我需要更好的方法.
谢谢.
示例代码
选项显式打开
公共类Form1
Dim _strFile As String =""
Dim _Reader As IO.StreamReader = Nothing
暗_ulngLine为ULong = 0
Dim _strLine As String =""
Public Class Form1
Dim _strFile As String = ""
Dim _Reader As IO.StreamReader = Nothing
Dim _ulngLine As ULong = 0
Dim _strLine As String = ""
私有Sub cmdOpenFile_Click(ByVal发送者作为对象,ByVal e作为System.EventArgs)处理cmdOpenFile.Click
将fd作为新的System.Windows.Forms.OpenFileDialog
Private Sub cmdOpenFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdOpenFile.Click
Dim fd As New System.Windows.Forms.OpenFileDialog
fd.Filter =可以记录文件(* .csv; *.txt)| * .csv; *.txt"
fd.FilterIndex = 1
fd.RestoreDirectory = True
fd.Filter = "Can log files (*.csv;*.txt)|*.csv;*.txt"
fd.FilterIndex = 1
fd.RestoreDirectory = True
如果fd.ShowDialog()= System.Windows.Forms.DialogResult.OK然后
If fd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
_strFile = fd.FileName
Me.Text = IO.Path.GetFileName(_strFile)
_Reader =新的IO.StreamReader(_strFile)
如果不是_Reader.EndOfStream然后
_ulngLine = _ulngLine +1
_strLine = _Reader.ReadLine
frmUpdate()
如果结束
其他
_strFile = fd.FileName
Me.Text = IO.Path.GetFileName(_strFile)
_Reader = New IO.StreamReader(_strFile)
If Not _Reader.EndOfStream Then
_ulngLine = _ulngLine + 1
_strLine = _Reader.ReadLine
frmUpdate()
End If
Else
退出子
如果结束
结束
Exit Sub
End If
End Sub
私人Sub cmdNextLine_Click(按对象发送ByByVal,按System.EventArgs接收ByVal e)处理cmdNextLine.Click
_ulngLine = _ulngLine +1
_strLine = _Reader.ReadLine
frmUpdate()
结束
Private Sub cmdNextLine_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdNextLine.Click
_ulngLine = _ulngLine + 1
_strLine = _Reader.ReadLine
frmUpdate()
End Sub
私有Sub cmdPreviousLine_Click(ByVal发送者作为对象,ByVal e作为System.EventArgs)处理cmdPreviousLine.Click
暗淡为ULong = 0
_Reader.Close()
_Reader =没什么
_Reader =新的IO.StreamReader(_strFile)
_ulngLine = _ulngLine-1
Private Sub cmdPreviousLine_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdPreviousLine.Click
Dim ulng As ULong = 0
_Reader.Close()
_Reader = Nothing
_Reader = New IO.StreamReader(_strFile)
_ulngLine = _ulngLine - 1
做
ulng = ulng + 1
_strLine = _Reader.ReadLine
ulng<>时循环_ulngLine
Do
ulng = ulng + 1
_strLine = _Reader.ReadLine
Loop While ulng <> _ulngLine
frmUpdate()
frmUpdate()
结束
End Sub
私人子frmUpdate()
lblLineNumber.Text = _ulngLine
lblLineText.Text = _strLine
Private Sub frmUpdate()
lblLineNumber.Text = _ulngLine
lblLineText.Text = _strLine
如果是_Reader.EndOfStream然后
cmdNextLine.Enabled =假
其他
cmdNextLine.Enabled = True
如果结束
If _Reader.EndOfStream Then
cmdNextLine.Enabled = False
Else
cmdNextLine.Enabled = True
End If
如果_ulngLine< 2然后
cmdPreviousLine.Enabled =假
其他
cmdPreviousLine.Enabled = True
如果结束
If _ulngLine < 2 Then
cmdPreviousLine.Enabled = False
Else
cmdPreviousLine.Enabled = True
End If
结束
End Sub
私有子Form1_Activated(ByVal发送者作为对象,ByVal e作为System.EventArgs)处理Me.Activated
cmdNextLine.Enabled =假
cmdPreviousLine.Enabled =假
结束
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
cmdNextLine.Enabled = False
cmdPreviousLine.Enabled = False
End Sub
私有子Form1_FormClosed(ByVal发送者作为对象,ByVal e作为System.Windows.Forms.FormClosedEventArgs)处理Me.FormClosed
_Reader.Close()
结束子
结束类
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
_Reader.Close()
End Sub
End Class
推荐答案
除非返回到开头,否则无法读取流中的前一行.这就是流的本质.
You can't read the previous line in a stream except by going back to the beginning. That's the nature of streams.
我能想到的两种可能的解决方案:
Two possible solution I can think of:
1读取下一行时,将前一行保存在字段中,并以上一行方法获取它.
1 When the next line is read save the previous one in a field and get that in previous line method.
2使用ReadAllLines将整个文件读入数组并使用它.如果文件太大,则可能无法实现.
2 Use ReadAllLines to read the complete file into an array and use that. May not be possible if the file is extremely large.
第三种方法是使用固定长度的线,并使用搜索"来获取所需的线,但我认为这不可行.
A third method would be to have fixed length lines and use Seek to get the line you want but I don't think that is feasible here.
这篇关于TextStream ReadPreviousLine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!