VB .Net读取文件中的特定数据 [英] VB .Net Read Specific Data In a File
问题描述
我需要帮助读取大文件中的特定数据。
i有包含 product id = id89et place等值的文本文件= india 等没有任何逗号分隔。
i需要读取此文件并在每个文本框中显示所有值。
请帮我看看如何阅读这个文件,我是新的vb.net编程,所以我没有任何想法执行这个任务。
请给我一些起点为它编码。
提前感谢。
如果您没有唯一的分隔符且没有修复列,那么你在如何做到这一点上非常有限。
例如,你的数据并不清楚你的行包含两个值:
product id = id89et
place = india我认为它确实存在,并且任何空格都会终止该值,但是在一行上只有两个条目并且注释等等它是一个危险的假设。
我会从正则表达式开始:将所有文件读成字符串,尝试这样的事情:
< pre lang =vb> 公共 Dim regex As Regex = 新正则表达式(_
(?< Key>。+?)=(?< Value>。+?)\ s,_
RegexOptions.IgnoreCase _
或 RegexOptions.Multiline _
或 RegexOptions.Singleline _
或 RegexOptions.CultureInvariant _
或 RegexOptions.IgnorePatternWhitespace _
或 RegexOptions.Compiled _
)
Dim ms As MatchC ollection = regex.Matches(InputText)
对于 每个 m 作为匹配在 ms
Console.WriteLine(m.Groups( 键)。值)
Console.WriteLine(m.Groups( < span class =code-string> Value)。Value)
Next
我发现解决方案是我自己..
首先我需要一个我的文件值的索引假设产品id = id09876543 location = india then index id09876543的值是12到23然后我将在函数调用中传递12和23.
1)使用户定义函数read_value传递2以整数形式在其中的参数,并将函数作为字符串,即它将以字符串格式返回值。
2)在您的函数中调用该函数你需要答案的特定地方。
喜欢这个。
1)
公共函数read_value(ByVal strat As Integer,ByVal end1 As Integer)As String
Dim fs As FileStream = New FileStream (f_name,FileMode.Open,FileAccess.Read)
Dim n As Integer = 0
Dim s As String = Nothing
Dim i As Integer = 0
Dim l As Long = strat
fs.Seek(l,SeekOrigin.Begin)
''寻求(策略)
for i = strat to end1
n = fs.ReadByte()
s = s + Convert.ToChar( n)
下一页
返回s
结束功能
2)
Dim ofd1作为新的OpenFileDialog''Dim file_name作为字符串尝试ifd1.ShowDialog = Windows.Forms.DialogResult.OK然后f_name = ofd1.FileName
product_id_txt.Text = read_value(12,23)
location_txt.Text = read_value(34,50)
form.Show()
结束如果
Catch ex As Exception
MessageBox.Show(File Not找到)
结束尝试
此代码的输出是
id09876543< ---- this是我的文本框值
这样做..
i need a help on reading a specific data in large file.
i have text file containing values such as product id= id89et place = india etc without any comma separated.
i need to read this file and displaying all values in a every text box.
please help me how to read this file, i am newly in vb.net programing so i haven''t any idea to perform this task.
please give me some starting point of coding for it.
thanks in advance.
If you have no unique separators and no fixed columns, then you are pretty limited in how you can do this.
for example, your data does not make it clear that your line consists of two values:
product id= id89et place = indiaI assume it does, and that any whitespace terminates the value, but with only two entries on a single line and the comment "etc" it is a dangerous assumption.
I would start with a regular expression: Read all the file into a string, try something like this:
Public Dim regex As Regex = New Regex( _ "(?<Key>.+?)=(?<Value>.+?)\s", _ RegexOptions.IgnoreCase _ Or RegexOptions.Multiline _ Or RegexOptions.Singleline _ Or RegexOptions.CultureInvariant _ Or RegexOptions.IgnorePatternWhitespace _ Or RegexOptions.Compiled _ ) Dim ms As MatchCollection = regex.Matches(InputText) For Each m As Match In ms Console.WriteLine(m.Groups("Key").Value) Console.WriteLine(m.Groups("Value").Value) Next
I found solution my self..
first of all i need a index of my file values suppose product id =id09876543 location =india then index values of id09876543 is " 12 to 23 " then i will pass 12 and 23 in function calling.
1) make a user define function called "read_value" pass the 2 argument in it in integer form and make function as string i.e. it will return value in string format.
2) call that function in your specific place where you want the answer.
like this.
1)
Public Function read_value(ByVal strat As Integer, ByVal end1 As Integer) As String
Dim fs As FileStream = New FileStream(f_name, FileMode.Open, FileAccess.Read)
Dim n As Integer = 0
Dim s As String = Nothing
Dim i As Integer = 0
Dim l As Long = strat
fs.Seek(l, SeekOrigin.Begin)
''Seek(strat)
For i = strat To end1
n = fs.ReadByte()
s = s + Convert.ToChar(n)
Next
Return s
End Function
2)
Dim ofd1 As New OpenFileDialog '' Dim file_name As String Try If ofd1.ShowDialog = Windows.Forms.DialogResult.OK Then f_name = ofd1.FileName
product_id_txt.Text = read_value(12, 23)
location_txt.Text = read_value(34, 50)
form.Show()
End If
Catch ex As Exception
MessageBox.Show("File Not Found")
End Try
Output of this code is
id09876543 <---- this is my text box value
this is done..
这篇关于VB .Net读取文件中的特定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!