vb.net Visual Basic 逐行读取文件,将每一行存储在 str 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13903245/
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
Visual Basic Read File Line by Line storing each Line in str
提问by townlong
I am trying to loop through the contents of a text file reading the text file line by line. During the looping process there is several times I need to use the files contents.
我试图遍历文本文件的内容,逐行读取文本文件。在循环过程中有几次我需要使用文件内容。
Dim xRead As System.IO.StreamReader
xRead = File.OpenText(TextBox3.Text)
Do Until xRead.EndOfStream
Dim linetext As String = xRead.ReadLine
Dim aryTextFile() As String = linetext.Split(" ")
Dim firstname As String = Val(aryTextFile(0))
TextBox1.Text = firstname.ToString
Dim lastname As String = Val(aryTextFile(0))
TextBox2.Text = lastname.ToString
Loop
Edit:What I am trying to do is read say the first five items in a text file perform some random processing then read the next 5 lines of the text file.
编辑:我想要做的是读取说文本文件中的前五个项目执行一些随机处理然后读取文本文件的下 5 行。
I would like to be able to use the lines pulled from the text file as separated string variables.
我希望能够使用从文本文件中提取的行作为分隔的字符串变量。
回答by Neolisk
It is not clear why you would need to have 5 lines stored at any time, according to your code sample, since you are only processing one line at a time. If you think that doing 5 lines at once will be faster - this is unlikely, because .NET maintains caching internally, so both approaches will probably perform the same. However, reading one line at a time is a much more simple pattern to use, so better look into that first.
根据您的代码示例,不清楚为什么您需要随时存储 5 行,因为您一次只处理一行。如果您认为一次执行 5 行会更快 - 这不太可能,因为 .NET 在内部维护缓存,因此两种方法可能会执行相同的操作。然而,一次阅读一行是一种更简单的使用模式,所以最好先研究一下。
Still, here is an approximate version of the code that does processing every 5 lines:
不过,这里是每 5 行处理一次的代码的近似版本:
Sub Main()
Dim bufferMaxSize As Integer = 5
Using xRead As New System.IO.StreamReader(TextBox3.Text)
Dim buffer As New List(Of String)
Do Until xRead.EndOfStream
If buffer.Count < bufferMaxSize Then
buffer.Add(xRead.ReadLine)
Continue Do
Else
PerformProcessing(buffer)
buffer.Clear()
End If
Loop
If buffer.Count > 0 Then
'if line count is not divisible by bufferMaxSize, 5 in this case
'there will be a remainder of 1-4 records,
'which also needs to be processed
PerformProcessing(buffer)
End If
End Using
End Sub
回答by Royal911
Here is mine . Rely easy . Just copy the location from the file and copy1 folder to does locations . This is my first program :) . ready proud of it
这是我的。靠容易。只需将文件和 copy1 文件夹中的位置复制到 do 位置。这是我的第一个程序 :) 。准备为它骄傲
Imports System.IO
进口系统.IO
Module Module1
模块模块1
Sub Main()
For Each Line In File.ReadLines("C:\location.txt".ToArray)
My.Computer.FileSystem.CopyDirectory("C:\Copy1", Line, True)
Next
Console.WriteLine("Done")
Console.ReadLine()
End Sub
End Module
终端模块

