检查文件逻辑中的第一个单词

Check first word in file logic

我正在尝试在打开文件的程序中创建一个部分,如果第一行的第一个单词是 "title" 那么我希望它将输入组写入文件,不带标题排。如果文件为空并且没有第一个单词 "title" 那么我希望它只写一次该行,然后继续将其他输入框写入文本文件。我不认为我离得太远,我只是需要另一双眼睛来审视我的逻辑。

谢谢!

我的输出是:

[Car]{bgcolor: ##fcecec}
*VIN{label: varchar, not null}
Make {label: varchar, not null}
Model {label: varchar, not null}
Year {label: varchar, not null}
Color {label: varchar, not null}
Miles {label: varchar, not null}

title {label: Database (condensed), size: 20}
[Owner]{bgcolor: ##ececfc}
*Fname{label: varchar, not null}
LName {label: varchar, not null}
Address {label: varchar, not null}
Phone {label: varchar, not null}
Employeer {label: varchar, not null}
Annual Income {label: varchar, not null}
Married {label: varchar, not null}
DOB {label: varchar, not null}

title {label: Database (condensed), size: 20}
[Employeer]{bgcolor: ##d0e0d0}
*Name{label: varchar, not null}
Address {label: varchar, not null}
Phone {label: varchar, not null}
Salary {label: varchar, not null}
Years with company {label: varchar, not null}
Job title {label: varchar, not null}

它应该是这样的:

title {label: Database (condensed), size: 20}
[Car]{bgcolor: ##fcecec}
*VIN{label: varchar, not null}
Make {label: varchar, not null}
Model {label: varchar, not null}
Year {label: varchar, not null}
Color {label: varchar, not null}
Miles {label: varchar, not null}

[Owner]{bgcolor: ##ececfc}
*Fname{label: varchar, not null}
LName {label: varchar, not null}
Address {label: varchar, not null}
Phone {label: varchar, not null}
Employeer {label: varchar, not null}
Annual Income {label: varchar, not null}
Married {label: varchar, not null}
DOB {label: varchar, not null}

[Employeer]{bgcolor: ##d0e0d0}
*Name{label: varchar, not null}
Address {label: varchar, not null}
Phone {label: varchar, not null}
Salary {label: varchar, not null}
Years with company {label: varchar, not null}
Job title {label: varchar, not null}

我的代码是:

 'database name

    ' Create an instance of StreamReader to read from a file.
    ' The using statement also closes the StreamReader.
    Using sr As New StreamReader(FILE_NAME)
        Dim line, firstWord As String
        Dim k As Integer = 0
        ' Read and display lines from the file until the end of
        ' the file is reached.
        Do
            line = sr.ReadLine()

            If Not (line Is Nothing) And k = 0 Then
                firstWord = line.Split(" ")(k)
                If firstWord.Contains("title") Then
                    sr.Close() ' closes file
                    GoTo AttributeWrite
                Else
                    sr.Close() ' closes file
                    Dim TitleWrite As New System.IO.StreamWriter(FILE_NAME, True) ' open file
                    TitleWrite.WriteLine("title {label: " & TextBox10.Text & " (condensed)" & ", size: " & "20" & "}") ' write to file
                    TitleWrite.Close() ' closes file


                End If
            End If
        Loop Until k = 0
    End Using

如果是文本文件,则File.ReadAllLines。创建一个字符串列表,然后删除索引(0) 处的字符串项。示例:

 Dimm AllData as new List(Of String)

 For each Line in FIle.ReadAllLines(filepathhere)
 AllData.Add(Line)
 Next
 AllData.RemoveAt(0) 'This will remove the first line only

或者如果title不在第一行,则找到它的index然后删除它:

Dim index As Integer = AllData.FindIndex(Function(a) a = "title")
AllData.RemoveAt(Index)

希望这对您有所帮助 :) .

休息一会再看,才知道哪里出了问题

我不得不移动我的位置 "end if"

感谢提供解决方案的人:)

 If Not (line Is Nothing) And k = 0 Then
                firstWord = line.Split(" ")(k)
                If firstWord.Contains("title") Then
                    sr.Close() ' closes file
                    GoTo AttributeWrite
                End If  <----- THIS ONE

                Else
                    sr.Close() ' closes file
                    Dim TitleWrite As New 
  System.IO.StreamWriter(FILE_NAME, True) ' open file
                    TitleWrite.WriteLine("title {label: " & 
  TextBox10.Text & " (condensed)" & ", size: " & "20" & "}") ' write to file
                    TitleWrite.Close() ' closes file


                End If