簡體   English   中英

在VB.NET中讀取數組中文本文件的特定行

[英]Reading a particular line of a text file in an array in VB.NET

我正在嘗試從數組中的文本文件讀取特定行(例如,行16,25,34,依此類推)。 您能否讓我知道是否可能,以及如何做到?

在此先感謝,Pouya

對的,這是可能的。 由於這不是基於代碼的,因此將詳細說明如何實現。 這將取決於目標文件的大小。 如果大小不適合PC的內存,那么您可以在讀取整個文本文件的同時讀取計數。

然后,在讀取文件后開始,以使用正則表達式結束您的工作。

校驗:

VB.NET讀取文本文件中的某些文本

您的解決方案在這里:

http://www.dreamincode.net/forums/topic/56497-go-to-a-particular-line-in-a-text-file-using-vbnet/

如何從VB中的文本文件讀取特定行

好的,這也是我在上面引用的代碼,從倒數第二個為您提供幫助。 我確定您知道如何從數組中獲取數據,因此您將添加數組而不是line

Public Function
  ReadSpecifiedLine(ByVal line As
  Integer) As String
 'create a variable to
 hold the contents of the file

 Dim contents As String = String.Empty
 'create a variable to
 hold our line contents

 Dim lineText As String =
 String.Empty
 ' always use a
 try...catch to deal
 ' with any exceptions
 that may occur
 Try

 'Using lineByLine As New IO.StreamReader(_fileName)

 Dim lineCount As Integer = 0
 While Not lineByLine.EndOfStream
 lineByLine.ReadLine ()
 If lineCount = line Then 
  ' you can replace the line variable above or use the And Or to match the lines from your array.
 lineText = lineByLine.ReadLine()
 End If

 lineCount += 1
 End While
 End Using

 Catch ex As FileNotFoundException
 lineText = String.Empty

 _returnMessage = ex.Message
 Catch ex As Exception

  ' deal with any errors
 _returnMessage = ex.Message
 End Try
 Return lineText
 End Function

希望能對您有所幫助。(很抱歉,在代碼格式化過程中遇到一些問題,某些部分可能無法成型或可見。如果看不到End Function,請參閱鏈接。我已經嘗試過很多次了,但未能正確成型) ,我正在使用手機。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM