Visual Basic写入文件错误

时间:2016-03-08 07:13:27

标签: vb.net readfile writefile

我正在开发一个相对简单的vb项目..从文本文件中读取行并将它们输出到新的文本文件。但是,当我在Visual Studio中运行此代码时,我不断收到错误“期望语句结束”(BC30205)和“pv_objText未声明”(BC30451)。我真的很不清楚错误在哪里。任何帮助将不胜感激,谢谢。

'Variables

    Const sFilePath As String = "Contacts.txt"
    Const c_sOutputFilePath As String = "ContactsReport.txt"
    Dim fileInput As StreamReader
    Dim fileOutput As StreamWriter
    Dim sText As String = ""
    Dim sDivider As String = "--------------------------------------------------------------------"


    'Begin Code

    SetTitle("Contacts2")

    fileInput = File.OpenText(sFilePath)
    fileOutput = File.CreateText(c_sOutputFilePath)

    While fileInput.Peek <> -1

        sText = fileInput.ReadLine
        fileOutput.WriteLine("Name: " & sText)
        fileOutput.WriteLine()

        sText = fileInput.ReadLine
        fileOutput.WriteLine("Address: ")
        sText = fileInput.ReadLine
        fileOutput.WriteLine(sText)
        sText = fileInput.ReadLine
        fileOutput.WriteLine(sText & ", ")
        sText = fileInput.ReadLine
        fileOutput.WriteLine(sText & " ")
        sText = fileInput.ReadLine
        fileOutput.WriteLine(sText)
        fileOutput.WriteLine()
        fileOutput.WriteLine()

        sText = fileInput.ReadLine
        fileOutput.WriteLine("Phone: " & sText)
        fileOutput.WriteLine()

        sText = fileInput.ReadLine
        fileOutput.WriteLine("Email: " & sText)
        fileOutput.WriteLine(sDivider)
        fileOutput.WriteLine()
        fileOutput.WriteLine()

    End While

    fileInput.Close()
    fileOutput.Close()

End Sub

End Module

modUtilities中的第57行是第一行中出现错误的地方:

(它也指的是pv_objText所在的第59行)

Public Sub fileOutput.WriteLine(Optional ByVal pv_objText As Object = "")

    Dim sText As String = CStr(pv_objText)

    fileOutputwrite(sText & vbNewLine)

End Sub

1 个答案:

答案 0 :(得分:0)

这是对您的方法的轻微改写。 根据您的行进行相应调整,看看它是否能解决您的问题。

   Private Sub ReadWriteContacts()

        Const sFilePath As String = "c:\test\Contacts.txt"
        Const c_sOutputFilePath As String = "c:\test\ContactsReport.txt"
        Const divider As String = "--------------------------------------------------------------------"

        Dim linePosition As Integer

        'include tests for exists output and delete it
        'include test for missing input and report it

        Using fileInput As StreamReader = New StreamReader(sFilePath)
            Using fileOutput As StreamWriter = File.CreateText(c_sOutputFilePath)

                Do Until fileInput.EndOfStream

                    Dim sText As String = fileInput.ReadLine
                    linePosition += 1
                    Select Case linePosition
                        Case 1
                            fileOutput.WriteLine("Name: " & sText)
                            fileOutput.WriteLine()

                        Case 2
                            fileOutput.WriteLine("Address: ")

                        Case 3
                            fileOutput.WriteLine(sText)

                        Case 4
                            fileOutput.WriteLine(sText & ", ")

                        Case 5
                            fileOutput.WriteLine(sText & " ")

                        Case 6
                            fileOutput.WriteLine(sText)
                            fileOutput.WriteLine()
                            fileOutput.WriteLine()

                        Case 7
                            fileOutput.WriteLine("Phone: " & sText)
                            fileOutput.WriteLine()

                        Case 8
                            fileOutput.WriteLine("Email: " & sText)
                            fileOutput.WriteLine(divider)
                            fileOutput.WriteLine()
                            fileOutput.WriteLine()
                    End Select

                Loop
            End Using
        End Using

    End Sub