如何从 VB.net 中的 .txt 文件中读取特定行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19537521/
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-17 15:35:36  来源:igfitidea点击:

How to read a specific line from a .txt file in VB.net

vb.net

提问by 08robertsj

I am currently trying to create a vocabulary testing program. What I am really trying to do is get the program to generate a random number, and get the program to choose a line of text from a .txtfile corresponding to the random number.

我目前正在尝试创建一个词汇测试程序。我真正想做的是让程序生成一个随机数,并让程序从与随机数对应的.txt文件中选择一行文本。

I've managed to get the random number to work, but I have no idea how to read a specific line from the .txtfile.

我设法让随机数起作用,但我不知道如何从.txt文件中读取特定行。

I'm fairly proficient with My.Computer.FileSystem.ReadAllText(filename, text, etc), but I can't seem to find any way of achieving my goal with it.

我相当精通My.Computer.FileSystem.ReadAllText(filename, text, etc),但我似乎找不到任何方法来实现我的目标。

If you can help, it would be preferable if the solution could avoid StreamReader.

如果您能提供帮助,如果解决方案可以避免StreamReader.

I would include some code, however its only a few lines long and contains only the random generator, so is pretty self explanatory.

我会包含一些代码,但是它只有几行长,并且只包含随机生成器,所以非常不言自明。

回答by Tim Schmelter

You can use System.IO.File.ReadAllLinesand the array indexer:

您可以使用System.IO.File.ReadAllLines和数组索引器:

Private Shared rnd As New Random()

Public Shared Function GetRandomLine(path As String) As String
    Dim allLines As String() = File.ReadAllLines(path)
    Dim randomLine As String = allLines(rnd.Next(allLines.Length))
    Return randomLine
End Function

Note that Random.Nextwith one parameter creates a random number from 0 until allLines.Length - 1(so exclusive upper bound ). That's correct, otherwise you'd get an IndexOutOfRangeExceptionexception.

请注意,Random.Next使用一个参数会创建一个从 0 到allLines.Length - 1(因此唯一的上限)的随机数。这是正确的,否则你会得到一个IndexOutOfRangeException例外。

回答by Joe Enos

If you're sure the file will be reasonably small, the easiest way to read lines from a textfile is to use:

如果您确定文件相当小,从文本文件中读取行的最简单方法是使用:

Dim lines = System.IO.File.ReadAllLines(fullFileName)

lineswill be a string array, so you can access a random line from that array.

lines将是一个字符串数组,因此您可以访问该数组中的随机行。

But if you're not sure that the file is small, and you don't want to risk running out of memory, a StreamReaderreally isn't that bad - it's designed to be easy to work with when you're reading textfiles, unlike FileStreamand some of the other methods, which are a little trickier.

但是,如果您不确定该文件是否小,并且您不想冒内存不足的风险,那StreamReader确实还不错 - 它的设计目的是在您阅读文本文件时易于使用,不像FileStream和其他一些方法,这些方法有点棘手。

First you'd need to know how many lines are in the file, so you'd have to read through the file once, then a second time once you've picked a random number.

首先,您需要知道文件中有多少行,因此您必须通读文件一次,然后在选择一个随机数后再次通读。

Dim numLines as Integer = 0
Using sr As New System.IO.StreamReader(fullFileName)
    Dim line As String = sr.ReadLine()
    While line IsNot Nothing
        numLines = numLines + 1
        line = sr.ReadLine()
    End While
End Using

Once you've determined how many lines there are, you can pick your random number, then repeat the process, stopping at the pre-determined line.

确定有多少行后,您可以选择随机数,然后重复该过程,在预先确定的行处停止。

回答by dbasnett

I have a post herethat provides an alternative to ReadAll methods. There is nothing wrong with the ReadAll methods as long as the file is relatively small and memory usage is not an issue, which is normally the case.

我在这里有一篇文章提供了 ReadAll 方法的替代方法。只要文件相对较小且内存使用不成问题,ReadAll 方法就没有问题,通常是这种情况。