Vb.net 如何在VB中从文本文件中读取特定行

Vb.net 如何在VB中从文本文件中读取特定行,vb.net,Vb.net,我正在创建一个程序,该程序应该将文本写入文本文件,并且应该能够在VB中读取文本文件中的特定行(因此,如果需要读取特定名称,我可以选择第5行,它将显示在文本框中)。我能够从文本文件中读取文本,但我不知道如何控制特定行 这是我的密码: Public Class Form1 Private Sub btnSubmit_Click(sender As System.Object, e As System.EventArgs) Handles btnSubmit.Click D

我正在创建一个程序,该程序应该将文本写入文本文件,并且应该能够在VB中读取文本文件中的特定行(因此,如果需要读取特定名称,我可以选择第5行,它将显示在文本框中)。我能够从文本文件中读取文本,但我不知道如何控制特定行

这是我的密码:

Public Class Form1

    Private Sub btnSubmit_Click(sender As System.Object, e As System.EventArgs) Handles btnSubmit.Click
         Dim writer As New System.IO.StreamWriter("/text.txt", True)
         writer.WriteLine(txtFirstName.Text)
         writer.WriteLine(txtLastName.Text)
         writer.WriteLine("-------------------------------------")
         writer.Close()
    End Sub

     Private Sub btnRead_Click(sender As System.Object, e As System.EventArgs) Handles btnRead.Click
        Dim reader As New System.IO.StreamReader("/text.txt")
        Dim FirstName, LastName As String
        FirstName = reader.ReadLine()
        LastName = reader.ReadLine()
        reader.Close()
        txtFirstName.Text = FirstName
        txtLastName.Text = LastName
    End Sub

    Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
        txtFirstName.Clear()
        txtLastName.Clear()
    End Sub
End Class

任何帮助都将不胜感激。谢谢

你必须把所有的台词都读到你感兴趣的那一行。例如:

Function ReadLineWithNumberFrom(filePath As String, ByVal lineNumber As Integer) As String
    Using file As New StreamReader(filePath)
        ' Skip all preceding lines: '
        For i As Integer = 1 To lineNumber - 1
            If file.ReadLine() Is Nothing Then
                Throw New ArgumentOutOfRangeException("lineNumber")
            End If
        Next
        ' Attempt to read the line you're interested in: '
        Dim line As String = file.ReadLine()
        If line Is Nothing Then
            Throw New ArgumentOutOfRangeException("lineNumber")
        End If
        ' Succeded!
        Return line 
    End Using
End Function
这是因为文本行是可变长度的记录,并且无法猜测特定行开始位置的确切文件偏移量-不是没有索引

如果您经常需要加载特定行,则有更多选项:

  • 将完整的文本文件加载到内存中,例如使用
    file.ReadAllLines(“Foobar.txt”)
    。这将返回一个
    String()
    数组,您可以通过行号直接访问该数组

  • 手动创建行号索引。也就是说,逐行处理一个文本文件,然后边走边填写
    字典(整数,整数)
    。关键点是行号,值是文件偏移。这允许您将
    .Seek
    直接搜索到特定行的开头,而不必将整个文件保存在内存中

试试这个:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim reader As New System.IO.StreamReader("C:\text.txt")
    Dim allLines As List(Of String) = New List(Of String)
    Do While Not reader.EndOfStream
        allLines.Add(reader.ReadLine())
    Loop
    reader.Close()
    txtFirstName.Text = ReadLine(5, allLines)
    txtLastName.Text = ReadLine(6, allLines)

End Sub

Public Function ReadLine(lineNumber As Integer, lines As List(Of String)) As String
    Return lines(lineNumber - 1)
End Function
如果您有此文件:

Line 1
Line 2
Line 3
Line 4
My Name
My LastName
您的姓名文本框将包含“我的姓名”,您的姓氏文本框将包含“我的姓氏”。

还有另一个选项

    Private Function readNthLine(fileAndPath As String, lineNumber As Integer) As String
    Dim nthLine As String = Nothing
    Dim n As Integer
    Try
        Using sr As StreamReader = New StreamReader(fileAndPath)
            n = 0
            Do While (sr.Peek() >= 0) And (n < lineNumber)
                sr.ReadLine()
                n += 1
            Loop
            If sr.Peek() >= 0 Then
                nthLine = sr.ReadLine()
            End If
        End Using
    Catch ex As Exception
        Throw
    End Try
    Return nthLine
End Function
Private Function readNthLine(fileAndPath作为字符串,lineNumber作为整数)作为字符串
字符串形式的第n行=无
作为整数的Dim n
尝试
使用sr作为StreamReader=新的StreamReader(fileAndPath)
n=0
Do While(sr.Peek()>=0)和(n=0,则
第n行=高级读取行()
如果结束
终端使用
特例
扔
结束尝试
回程线
端函数

这很简单,请尝试以下方法:

Dim strLineText As String
Dim intLineNumber As Integer
LineNumber=3

myLine = File.ReadAllLines("D:\text.txt").ElementAt(LineNumber).ToString

我试过这个,效果很好。使用vbexpress test.txt中的内容:

line1
第2行
1.
约翰
然后以我添加的形式

textbox1
textbox2
label1
label2
和一个
按钮

按钮内的代码:

     Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
        Dim myLine As String
        Dim lineNumber0 As Integer
        lineNumber0 = 0
        Dim lineNumber1 As Integer
        lineNumber1 = 1
        Dim lineNumber2 As Integer
        lineNumber2 = 2
        Dim lineNumber3 As Integer
        lineNumber3 = 3

        TextBox1.Text=File.ReadAllLines("D:\test.txt").ElementAt(lineNumber0).ToString
        TextBox2.Text=File.ReadAllLines("D:\test.txt").ElementAt(lineNumber1).ToString
        Label1.Text = File.ReadAllLines("D:\test.txt").ElementAt(lineNumber2).ToString
        Label2.Text = File.ReadAllLines("D:\test.txt").ElementAt(lineNumber3).ToString

    End Sub

我做了一些非常类似的事情,但这也会起作用。我最终读取了文件中的行数,然后使用了一种算法,可以跳转到所需的特定行。然后在那一行之后阅读需要的内容。很抱歉,我花了这么长时间才找到你,我在别人回来之前就把它修好了。谢谢你的帮助!伟大的解决方案,今天遇到了这一点,工作了一顿款待,并确保我在代码中归功于您:-)有一个问题,我知道这是旧的,但仍然。你能举例说明如何使用这个吗?我在一个文件中有大约5行文本,例如,我需要删除第2行的文本。我该怎么做?谢谢@Rootel:如果您的文本文件很小,我建议您保持简单:
将行的大小设置为String()=System.IO.File.ReadAllLines(文件名):将第二行的大小设置为String=lines(1)
。如果您想要我的答案中的方法:
Dim secondLine As String=..ReadLineWithNumberFrom(fileName,2)
。(我想。我已经很久没有考虑上面显示的代码了。)您显然需要在
模块
中定义所显示的函数。在前一种情况下,您必须
新建
对象实例,您可以调用该方法。我发现这对我正在处理的项目非常有用,我需要输入文件中特定行上的值。一个非常清楚的解释,一个完美的例子。谢谢。工作得很好,但速度很慢,你知道有什么办法可以加快速度吗?