如何编辑文本文件并删除值?

时间:2012-06-21 14:34:12

标签: .net vb.net vb.net-2010

我有两个文本文件。每个文本文件中的字段用空格(“”)分隔。文件1的第1列中的某些字段与文件2的第1列中的字段匹配。但是文件2中的第3列是数字字段。我想要做的是检查file1中的每个字段与文件2中的字段,如果数字是1,则从文件2中删除该行,如果该数字是> 1,然后从中减去一个。

到目前为止,我有以下编码。

Dim lines1 As New List(Of String)(IO.File.ReadAllLines("File1"))
Dim lines2 As New List(Of String)(IO.File.ReadAllLines("File2"))

Dim values As New Dictionary(Of String, Integer)()
For Each line As String In lines1
    Dim fields() As String = line.Split(ControlChars.Tab)
    If fields.Length > 1 Then
        values(fields(0)) = Integer.Parse(fields(1))
    End If
Next

For Each line As String In lines2
    Dim fields() As String = line.Split(ControlChars.Tab)
    If fields.Length > 0 Then
        If values.ContainsKey(fields(0)) > 1 Then
            values(fields(0)) = values(fields(0)) - 1
        Else
            values.remove(fields(0))
        End If
    End If
Next

lines1.Clear()
For Each pair As KeyValuePair(Of String, Integer) In values
    lines1.Add(pair.Key + ControlChars.Tab + pair.Value.ToString())
Next

IO.File.WriteAllLines("File2", lines1.ToArray)

例如

File1

String1 String 
String2 String
String5 String
String8 String

File2

String1 String 6
String2 String 8
String3 String 2
String4 String 2
String5 String 1
String6 String 4
String7 String 8
String8 String 1

我的代码运行后

File1

String1 String 
String2 String
String5 String
String8 String

File2

String1 String 5
String2 String 7
String3 String 2
String4 String 2
String6 String 4
String7 String 8

1 个答案:

答案 0 :(得分:1)

这里有一些解析乐趣......只需将strFile1Path和strFile2Path与各自的文件路径一起调低,然后让这段代码处理其余部分。希望代码和评论会教你一些技巧。

    Dim lstFile1Contents As New List(Of String)(IO.File.ReadAllLines(strFile1Path))
    Dim lstFile2Contents As New List(Of String)(IO.File.ReadAllLines(strFile2Path))

    Dim sbNewFile2Contents As New System.Text.StringBuilder

    For Each strLineToProcess As String In lstFile2Contents

        'Trim off trailing spaces for processing.
        strLineToProcess = Trim(strLineToProcess)

        Dim strCheckForMatch As String = strLineToProcess.Substring(0, (InStr(strLineToProcess, " ") - 1))

        Dim bolFoundMatch As Boolean = False
        Dim intCursor As Integer = 0
        Do Until intCursor = lstFile1Contents.Count OrElse bolFoundMatch

            If lstFile1Contents(intCursor).Substring(0, (InStr(lstFile1Contents(intCursor), " ") - 1)) = strCheckForMatch Then

                bolFoundMatch = True

                'We found a match, so let's check the third field.
                Dim intNumber As Integer = CInt(strLineToProcess.Substring((strLineToProcess.LastIndexOf(" ") + 1), (Len(strLineToProcess) - (strLineToProcess.LastIndexOf(" ") + 1))))

                If intNumber > 1 Then

                    'Subtract one from the third field.
                    sbNewFile2Contents.AppendLine(strLineToProcess.Substring(0, (strLineToProcess.LastIndexOf(" ") + 1)) & (intNumber - 1).ToString())

                End If

            End If

            intCursor += 1

        Loop

        If Not bolFoundMatch Then

            'No match was found, so make sure the line remains unedited.
            sbNewFile2Contents.AppendLine(strLineToProcess)

        End If

    Next

    'Finally write the file contents.
    IO.File.WriteAllText(strFile2Path, sbNewFile2Contents.ToString())