vb.net StreamReader - 从行中读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3923613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:04:35  来源:igfitidea点击:

StreamReader - Reading from lines

vb.net

提问by developer__c

I have the following code;

我有以下代码;

Public Sub writetofile()

  ' 1: Append playername
    Using writer As StreamWriter = New StreamWriter("highscores.txt", True)
        writer.WriteLine(PlayerName)
    End Using

    ' 2: Append score
    Using writer As StreamWriter = New StreamWriter("highscores.txt", True)
        writer.WriteLine(Score)
    End Using

End Sub

What I now want to do is read all the odd lines of the file (the player names) and the even lines into two separate list boxes, how would I go about that??

我现在想要做的是将文件的所有奇数行(玩家名称)和偶数行读入两个单独的列表框中,我该怎么做?

I need to modify;

我需要修改;

Using reader As StreamReader = New StreamReader("file.txt")
            ' Read one line from file
            line = reader.ReadLine
        End Using

I have used one of the following solutions but cannot get it working :(

我使用了以下解决方案之一,但无法使其正常工作:(

Public Sub readfromfile()
    Using reader As New StreamReader("scores.txt", True)
        Dim line As Integer = 0
        While Not reader.EndOfStream
            If line Mod 2 = 0 Then
                frmHighScores.lstScore.Items.Add(line)
            Else
                frmHighScores.lstScore.Items.Add(line)
            End If
            line += 1
        End While
    End Using
End Sub

回答by Fredrik M?rk

You can use the Modoperator for this:

您可以Mod为此使用运算符:

Using reader As New StreamReader("highscores.txt", True)
    Dim line As Integer = 0
    Dim text As String
    Do
        text = reader.ReadLine()
        If text = Nothing Then
            Exit Do
        End If

        If line Mod 2 = 0 Then
            ''# even line
        Else
            ''# odd line                        
        End If
        line += 1
    Loop
End Using

This approach also works for cases when it's not an even/odd pattern, but another number of repetions. Say you have 3 lines for each player:

这种方法也适用于它不是偶数/奇数模式,而是另一种重复次数的情况。假设每个玩家有 3 行:

player name 1
score 1
avatar url 1
player name 2
score 2
avatar url 2
...

Then you can get this pattern by using Modwith 3

然后你可以通过使用Modwith 3来获得这个模式

Dim subLine As Integer = line Mod 3
If  subLine = 0 Then
    ''# player name
ElseIf subLine = 1 Then
    ''# score                 
Else
    ''# avatar url
End If
line += 1

回答by Adam Robinson

If you can reliably expect there to be an even number of lines in the file, then you can simplify this by reading two at a time.

如果您可以可靠地预期文件中有偶数行,那么您可以通过一次读取两行来简化这一点。

Using reader As StreamReader = New StreamReader("file.txt")
    While Not reader.EndOfStream
        Dim player as String = reader.ReadLine()
        Dim otherInfo as String = reader.ReadLine()

        'Do whatever you like with player and otherInfo
    End While
End Using

回答by Preet Sangha

I don't really know the syntax of VB but something like this:

我真的不知道 VB 的语法,但像这样:

dim odd as boolean = True
Using reader As StreamReader = New StreamReader("file.txt")
            line = reader.ReadLine
            if odd then 
                ' add to list A
            else
                ' add to list B
            end
            odd = Not Odd
End Using

回答by DarinH

Another possibility is to just read the whole file as a block into a single string and the SPLIT the string on vbcrlf

另一种可能性是将整个文件作为一个块读取到一个字符串中,然后在 vbcrlf 上拆分该字符串

    Dim buf = My.Computer.FileSystem.ReadAllText(Filename)
    Dim Lines() = Split(Buf, vbcrlf)

Then, lines will contain all the lines from the file, indexed.

然后,行将包含文件中的所有行,索引。

So you could step through them to get each player and his other info.

所以你可以通过它们来获取每个玩家和他的其他信息。

 For x = 0 to ubound(Lines)
       'do whatever with each line
 next        

If the file was HUGE, you wouldn't necessarily want to do it this way, but for small files, it's a quick and easy way to handle it.

如果文件很大,您不一定要这样做,但对于小文件,这是一种快速简便的处理方法。