vb.net 从文本文件读取到数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47995239/
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
Reading from text file to array
提问by finn3y
I am trying to read a text file in line by line and add it to an array, current code and output are below. How would I read it in properly, i.e. get the actual text to read into the array, rather than the current output. (Console App version of VB.NET)
我正在尝试逐行读取文本文件并将其添加到数组中,当前代码和输出如下。我将如何正确读取它,即获取实际文本以读入数组,而不是当前输出。(VB.NET的控制台应用程序版本)
Code:
代码:
Sub Main()
Dim file As String = "C:\path\to\file\textfile.txt"
Dim quoteArray As New ArrayList
FileOpen(1, file, OpenMode.Input)
Do While Not EOF(1)
quoteArray.Add(LineInput(1))
Loop
FileClose(1)
Console.WriteLine(quoteArray)
Console.ReadLine()
End Sub
Output:
输出:
System.Collections.ArrayList
回答by Visual Vincent
Your code works, but you cannot print an entire array at once. You've got to iterate the array somehow and print each item separately or combine them into a single string.
您的代码有效,但您无法一次打印整个数组。您必须以某种方式迭代数组并分别打印每个项目或将它们组合成一个字符串。
Printing each item separately:
分别打印每个项目:
For Each Item As String In quoteArray
Console.WriteLine(Item)
Next
Combining them to a single string using String.Join():
使用以下命令将它们组合成一个字符串String.Join():
Console.WriteLine(String.Join(Environment.NewLine, quoteArray.ToArray(GetType(String))))
However what I don't understand is why you are writing in VB.NET, but are still using outdated functions and classes from the VB6 era:
然而我不明白的是为什么你在 VB.NET 中编写,但仍然使用 VB6 时代的过时函数和类:
ArrayListFileOpen()LineInput()FileClose()
ArrayListFileOpen()LineInput()FileClose()
There are muchbetter alternatives these days:
有很多更好的选择,这些天:
ArrayListcan be replaced by aList(Of T)FileOpen()andFileClose()with aUsingblockcombined with aStreamReaderLineInput()withStreamReader.ReadLine()
ArrayList可以用一个代替List(Of T)FileOpen()并FileClose()与一个Using块结合StreamReaderLineInput()和StreamReader.ReadLine()
Or you can replace all the above with a regular arrayand a single call to File.ReadAllLines().
或者,您可以使用常规数组和对File.ReadAllLines().
StreamReadersolution:Dim quoteList As New List(Of String) Using Reader As New StreamReader("C:\path\to\file\textfile.txt") While Reader.EndOfStream = False quoteList.Add(Reader.ReadLine()) End While End UsingFile.ReadAllLines()solution:Dim quoteArray As String() = File.ReadAllLines("C:\path\to\file\textfile.txt")
StreamReader解决方案:Dim quoteList As New List(Of String) Using Reader As New StreamReader("C:\path\to\file\textfile.txt") While Reader.EndOfStream = False quoteList.Add(Reader.ReadLine()) End While End UsingFile.ReadAllLines()解决方案:Dim quoteArray As String() = File.ReadAllLines("C:\path\to\file\textfile.txt")
Printing the list/array using a loop:
使用循环打印列表/数组:
For Each Item As String In quoteArray
Console.WriteLine(Item)
Next
Printing the list/array using String.Join():
使用String.Join()以下方法打印列表/数组:
Console.WriteLine(String.Join(Environment.NewLine, quoteArray))
(if you are using the quoteListsolution just replace quoteArraywith quoteListin these two examples)
(如果您正在使用quoteList解决方案,只需在这两个示例中替换quoteArray为quoteList)
回答by JohnyL
Use ReadLines:
使用ReadLines:
The ReadLines and ReadAllLines methods differ as follows: When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned; when you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array. Therefore, when you are working with very large files, ReadLines can be more efficient.
ReadLines 和 ReadAllLines 方法的区别如下: 使用 ReadLines 时,可以在返回整个集合之前开始枚举字符串集合;使用 ReadAllLines 时,必须等待整个字符串数组返回,然后才能访问该数组。因此,当您处理非常大的文件时,ReadLines 会更有效率。

