【问题标题】:Edit TextFile after written in VB用VB编写后编辑文本文件
【发布时间】:2015-08-12 06:06:11
【问题描述】:

我正在使用 Visual Basic 2010 制作一个模拟停车的程序。 新车到货时,我将文本写入 txt 文件,格式如下:

#

车牌

全名

到达日期

到达时间

出发日期

出发时间

#

然后继续上面的格式。

我如何让该人通过输入他们的车牌找到该部分,然后显示该部分,以便他们输入他们的出发详细信息?

谢谢!

【问题讨论】:

  • 请说清楚。这些字段已经写在txt文件中了吗?并且您想通过 LincensePlate 检索数据?
  • 是的,我想显示整个部分(在“#”之间)并在单独的文本框中显示该部分中的所有内容...
  • 所以你的意思是会有LicensePlate:00001 FullName:MyName ArrivalDate:08122015 ArrivalTime:0500 DepartDate:08122015 DepartTime:08132015,你想检索00001, MyName... 等?
  • 没错,但他们不会事先有名字,只有数字和名字。
  • 我明白你的意思了,请看下面我的回答。

标签: vb.net file search text edit


【解决方案1】:

不要在普通的 TXT 文件中写入数据。而是使用 XML 文件。您可以使用 XML 解析器来加快获取速度。

【讨论】:

  • 感谢您的回复,但是我需要使用TXT,因为朋友想要在TXT中使用它。
  • 然后您可以阅读所有文本并根据需要将其拆分,然后找到 id。并将这段文字留在你的记忆中,这样你就不需要一直阅读。
【解决方案2】:

这是一个答案。我使用控制台作为示例。请参阅下面的代码。

Imports System.Text
Imports System.IO

Module Module1

    Sub Main()
        Dim blnFound As Boolean = False

        Console.WriteLine("LicensePlate: ")
        Dim strLicensePlate As String = Console.ReadLine()

        Dim text As String = File.ReadAllText("TextData.txt")
        Dim index As Integer = text.IndexOf("" & strLicensePlate)
        If index >= 0 Then
            blnFound = True
        End If

        If blnFound = False Then
            Console.WriteLine("FullName: ")
            Dim strFullName As String = Console.ReadLine()
            Console.WriteLine("ArrivalDate: ")
            Dim strArrivalDate As String = Console.ReadLine()
            Console.WriteLine("ArrivalTime: ")
            Dim strArrivalTime As String = Console.ReadLine()

            Using w As StreamWriter = File.AppendText("TextData.txt")
                WriteData(strLicensePlate, strFullName, strArrivalDate, strArrivalTime, w)
            End Using
        Else
            Console.WriteLine("DepartureDate: ")
            Dim strDepartureDate As String = Console.ReadLine()
            Console.WriteLine("DepartureTime: ")
            Dim strDepartureTime As String = Console.ReadLine()


            Dim lines() As String = File.ReadAllLines("TextData.txt")
            For i As Integer = 0 To lines.Length - 1
                If lines(i).Contains(strLicensePlate) Then
                    lines(i + 4) = "DepartureDate:" & strDepartureDate
                    lines(i + 5) = "DepartureTime:" & strDepartureTime
                    File.WriteAllLines("TextData.txt", lines)
                    Exit For
                End If
            Next
            Display(strLicensePlate)
        End If

        Console.ReadKey()
    End Sub

    Private Sub WriteData(ByVal strLicensePlate As String, ByVal strFullName As String, ByVal strArrivalDate As String, _
                    ByVal strArrivalTime As String, ByVal w As TextWriter)
        w.WriteLine("#")
        w.WriteLine("LicensePlate: {0}", strLicensePlate)
        w.WriteLine("FullName: {0}", strFullName)
        w.WriteLine("ArrivalDate: {0}", strArrivalDate)
        w.WriteLine("ArrivalTime: {0}", strArrivalTime)
        w.WriteLine("DepartureDate:")
        w.WriteLine("DepartureTime:")
    End Sub

End Module

WriteData 方法用于在汽车到达时写入数据。如您所见,DepartureDateDepartureTime 为空。获取text.IndexOf就是知道输入的LicensePlate是否已经存在。如果它还不存在,则索引返回-1。如果存在,则要求用户提供出发日期和时间。并且空白的出发日期和时间将替换为新输入的日期和时间。

File.ReadAllLines()逐行获取txt数据,逐行搜索使用lines(indexNumber).Contains的关键字。如果lines(indexNumber).Contains 返回true,则开始使用此代码将空的出发日期和时间替换为新输入的数据,

lines(i + 4) = "DepartureDate:" & strDepartureDate
lines(i + 5) = "DepartureTime:" & strDepartureTime

索引号还有额外的 4 和 5,因为索引当前指向 LicensePlate 的位置,而 DepartureDateDepartureTime45 行下方的 LicensePlate分别。

补充:

Private Sub Display(ByVal strLicensePlate As String)
    Dim lines() As String = File.ReadAllLines("TextData.txt")
    For i As Integer = 0 To lines.Length - 1
        If lines(i).Contains(strLicensePlate) Then
            Console.WriteLine(lines(i + 1))
            Console.WriteLine(lines(i + 2))
            Console.WriteLine(lines(i + 3))
            Console.WriteLine(lines(i + 4))
            Console.WriteLine(lines(i + 5))
            File.WriteAllLines("TextData.txt", lines)
            Exit For
        End If
    Next
End Sub

上面的代码正在显示数据。只需将Console.WriteLine 替换为您想要放置数据的任何变量textboxlabel。并且不要忘记在else 部分之后添加Display(strLicensePlate) For Next

【讨论】:

  • 感谢您的回复,但我只想在 TXT 文件中搜索车牌字符串,然后将该行以及下面的 5 行显示到单独的文本框中,我可以编辑。
  • 我是个初学者,你能帮我介绍一下吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-22
  • 1970-01-01
相关资源
最近更新 更多