After some thinking, I think I figured out how to read delimited text. I was just curious as to whether it'd be better to use StreamReader.ReadLine or File.ReadAllLine or if it matters at all? I'm assuming the ReadLine method would be better if I'm working with a larger file since ReadAllLine would store the entire thing in memory?
Here's my code - I'm reading from a text file and displaying into a listview. I'm still new to vb so let me know if there's a more efficient way of doing this or if there's a problem with how I'm doing it now.
File.ReadAllLines
Code:Dim lines() As String Dim parts() As String lines = File.ReadAllLines("C:\blah.txt") For Each line As String In lines parts = line.Split("|") Dim li As New ListViewItem li.Text = parts(0) li.SubItems.Add(parts(1)) li.SubItems.Add(parts(2)) li.SubItems.Add(parts(3)) li.SubItems.Add(parts(4)) ListView1.Items.Add(li) Next
StreamReader
Code:Dim lines As String Dim parts() As String Dim fs As New FileStream("C:\blah.txt", FileMode.Open) Dim sr As New StreamReader(fs) Do Until sr.EndOfStream lines = sr.ReadLine parts = lines.Split("|") Dim li As New ListViewItem li.Text = parts(0) li.SubItems.Add(parts(1)) li.SubItems.Add(parts(2)) li.SubItems.Add(parts(3)) li.SubItems.Add(parts(4)) ListView1.Items.Add(li) Loop




Reply With Quote