读取文本文件并将多行输出到文本框

时间:2010-11-03 18:38:54

标签: vb.net visual-studio-2008 textbox while-loop

我正在尝试读取包含多行的文本文件,然后将其显示在文本框中。问题是我的程序只读了一行。有人能指出我的错误吗?

Imports System.IO
Imports Microsoft.VisualBasic.FileIO

Public Class Form1

    Private BagelStreamReader As StreamReader
    Private PhoneStreamWriter As StreamWriter
    Dim ResponseDialogResult As DialogResult

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click

        'Dim PhoneStreamWriter As New StreamWriter(OpenFileDialog1.FileName)

        'Is file already open
        If PhoneStreamWriter IsNot Nothing Then
            PhoneStreamWriter.Close()
        End If

        With OpenFileDialog1
            .InitialDirectory = Directory.GetCurrentDirectory
            .FileName = OpenFileDialog1.FileName
            .Title = "Select File"
            ResponseDialogResult = .ShowDialog()
        End With

        'If ResponseDialogResult <> DialogResult.Cancel Then
        '    PhoneStreamWriter = New StreamWriter(OpenFileDialog1.FileName)
        'End If

        Try
            BagelStreamReader = New StreamReader(OpenFileDialog1.FileName)
            DisplayRecord()
        Catch ex As Exception
            MessageBox.Show("File not found or is invalid.", "Data Error")
        End Try  
    End Sub

    Private Sub DisplayRecord()

        Do Until BagelStreamReader.Peek = -1
            TextBox1.Text = BagelStreamReader.ReadLine()
        Loop

        'MessageBox.Show("No more records to display.", "End of File")
        'End If
    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click

        With SaveFileDialog1
            .InitialDirectory = Directory.GetCurrentDirectory
            .FileName = OpenFileDialog1.FileName
            .Title = "Select File"
            ResponseDialogResult = .ShowDialog()
        End With

        PhoneStreamWriter.WriteLine(TextBox1.Text)
        With TextBox1
            .Clear()
            .Focus()
        End With
        TextBox1.Clear()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click

        Dim PhoneStreamWriter As New StreamWriter(OpenFileDialog1.FileName)
        PhoneStreamWriter.Close()
        Me.Close()

    End Sub
End Class

这是一个示例文本文件:

  

香蕉坚果
  蓝莓
  肉桂
  鸡蛋
  平原
  罂粟籽
  南瓜
  黑麦
  盐
  芝麻种子

2 个答案:

答案 0 :(得分:4)

你可能只得到文件中的最后一行,对吗?您的代码每次都将TextBox1.Text设置为等于BagelSteramReader.ReadLine(),覆盖TextBox1.Text的先前值。试试TextBox1.Text += BagelStreamReader.ReadLine() + '\n'

编辑:虽然我必须窃取同意Hans Passant对此的评论;如果你想要一个更有效的算法,File.ReadAllLines()甚至可以节省你的时间和金钱......虽然我自己也不知道。 Darn .NET,拥有如此众多的功能......

答案 1 :(得分:0)

我写了一个程序来写入和读取文本文件。要将列表框的行写入文本文件,我使用了以下代码:

Private Sub txtWriteToTextfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWriteToTextfile.Click

    Dim FileWriter As StreamWriter

        FileWriter = New StreamWriter(FileName, False)

        ' 3. Write some sample data to the file.
        For i = 1 To lstNamesList.Items.Count
            FileWriter.Write(lstNamesList.Items(i - 1).ToString)
            FileWriter.Write(Chr(32))
        Next i

        FileWriter.Close()

End Sub

要读取和写入文本文件的内容并写入多行文本框(您只需要将文本框的多行属性设置为true),我使用了以下代码。我还需要做一些额外的编码来打破我从文本文件中收到的长字符串中的单个单词。

Private Sub cmdReadFromTextfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReadFromTextfile.Click
    Dim sStringFromFile As String = ""
    Dim sTextString As String = ""
    Dim iWordStartingPossition As Integer = 0
    Dim iWordEndingPossition As Integer = 0
    Dim iClearedTestLength As Integer = 0

    Dim FileReader As StreamReader


        FileReader = New StreamReader(FileName)

        sStringFromFile = FileReader.ReadToEnd()

        sTextString = sStringFromFile

        txtTextFromFile.Text = ""

        Do Until iClearedTestLength = Len(sTextString)

            iWordEndingPossition = CInt(InStr((Microsoft.VisualBasic.Right(sTextString, Len(sTextString) - iWordStartingPossition)), " "))

            txtTextFromFile.Text = txtTextFromFile.Text & (Microsoft.VisualBasic.Mid(sTextString, iWordStartingPossition + 1, iWordEndingPossition)) & vbCrLf

            iWordStartingPossition = iWordStartingPossition + iWordEndingPossition

            iClearedTestLength = iClearedTestLength + iWordEndingPossition

        Loop

        FileReader.Close()


End Sub