我在txt中有一个日志文件,里面是信息字符串。一个字符串看起来像这样:
49000 120614 12334480 12 32 2 90 90 90 321 384 2 345 873 890
空格之间的每个值代表特定的东西,(例如,第二行“120614”是日期“2014年6月12日”)我正在编写一个程序,我将txt文件加载到其中,然后当我单击一个某个按钮它将采用此字符串并将所有值放入特定的文本框中,例如“Date = 12 06 2014”。我一直在研究如何找到某条线并将其内容声明为该内容,但我还没有找到答案!我正在使用Visual Basic来编写它,我真的需要帮助以确定方向,stramreader能够做到这一点?我在寻找什么功能?
这是我加载文件的代码..只是加入。
Public Class Form1
Private Sub browsebtn1_Click(sender As Object, e As EventArgs) Handles browsebtn1.Click
Dim filedialog As New OpenFileDialog 'openfiledialog1 is now filedialog'
filedialog.Filter = "Text Document|*.txt" 'filter the openfiledialogs file extension to txt only'
filedialog.Title = "Select Bosvark Log File.." 'openfiledialog title'
If filedialog.ShowDialog = Windows.Forms.DialogResult.OK Then 'if the file is chosen then..'
filepath1.Text = filedialog.FileName 'filepath1 text is file path of selected file'
RichTextBox1.LoadFile(filepath1.Text, RichTextBoxStreamType.PlainText) 'richtextbox1 retrieves the file path and displays the document'
End If
End Sub
Private Sub convertbtn_Click(sender As Object, e As EventArgs) Handles convertbtn.Click
If RichTextBox1.Text = "" Then
MessageBox.Show("Please load a file first!") 'If user doesnt load a file, give them an error message.'
End If
End Sub
Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
If RichTextBox1.Text = "" Then
RichTextBox1.Visible = False 'If there is no file loaded, The textbox will not appear.'
Else
RichTextBox1.Visible = True 'if the file is loaded, textbox will appear.'
End If
End Sub
End Class
答案 0 :(得分:1)
您可以将文件的内容加载到字符串变量(如果文件太大)
Dim intFile As Integer
Dim strFile As String
Dim strData As String
intFile = FreeFile
strFile = "c:\temp\file.txt"
Open strFile For Input As #intFile
strData = Input(LOF(intFile), #intFile)
Close #intFile
您可以将所有数据拆分为每行数组
Dim strLine() As String
strLine = Split(strData)
你可以遍历所有行来对所有行执行某些操作
Dim lngLine As Long
For lngLine = 0 To UBound(strLine)
'do action
Next lngLine
或只是在一个特定的行上执行操作:
dim strField() as String
'split line into array separated per spaces
strField = Split(strLine(lngLine), " ")