Click here to Skip to main content
15,065,324 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to import the .txt file and segragate the nibbles(3 nibbles) and store the nibbles in an array for further processing.
i have written a code to import the .txt file to richtextbox and separate 3 nibbles and store it in array.

the below code which i have attached is working fine for small .txt file(< 100KB) but if file size is more for ex.: 2MB it is taking more time( 30 minutes ) to copy the nibbles to array.

kindly help me out to sort this problem.

the usual .txt file will be 2MB.

thanks in advance.

What I have tried:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        chk = 1


        Try
            Dim TextBox1 As String : TextBox1 = "D:\temp.txt"

            Dim dialog As New OpenFileDialog()

            If DialogResult.OK = dialog.ShowDialog Then
                TextBox1 = dialog.FileName
            End If


            Dim lines() As String = System.IO.File.ReadAllLines(TextBox1)

            For Each line As String In lines
                RichTextBox1.AppendText(line)
            Next

     
            Dim dat(RichTextBox1.TextLength) As String

            For i As Integer = 0 To RichTextBox1.TextLength - 1

                dat(i) = (RichTextBox1.Text(i) & RichTextBox1.Text(i + 1) & 
                          RichTextBox1.Text(i + 2))
                i += 2

            Next
            MessageBox.Show("Done")

        Catch ex As Exception
            MessageBox.Show(ex.Message, Nothing, MessageBoxButtons.OK, 
                               MessageBoxIcon.Error)
        End Try

    End Sub
Posted
Updated 20-May-21 1:12am
Comments
Jo_vb.net 20-May-21 8:34am
   
Why reading line after line to the RTB?

Try .LoadFile

https://kixforms.net/docs/chm/index.htm?page=source%2Frichtextboxloadfilemethod.htm

1 solution

Why are you using a RichTextBox just to process some data which you then throw away? The lines array has a Length property that tells you how many lines it has read. You can then build your dat list direct from the lines. No need for any text boxes.
   
Comments
Member 12659926 20-May-21 7:21am
   
thanks for reply...
the data which i have imported from txt file is not thrown away,it is required for further processing
the above code which i have done is working fine with less file sizes(<100KB),i want to know how i can optimize the above code so that it works for bigger files(>2MB).
or is their any other way that i can work on
Richard MacCutchan 20-May-21 7:31am
   
Like I said, don't use a RichTextBox. The time and space required to rebuild it every time you add a line, will grow exponentially as more text is added. And it is not clear what you are doing with the dat list.
Member 12659926 20-May-21 7:38am
   
I am reading from richtextbox and copying it to array.the below line does that...

dat(i) = (RichTextBox1.Text(i) & RichTextBox1.Text(i + 1) &
RichTextBox1.Text(i + 2))

for example.: if data in richtextbox is abc1234de45678
i want as dat(0) = abc dat(1) = 123... etc

the above code which i am copying to array is time consuming.

so can u suggest any alternatives approach or any example the does the job with dat list
i dont have much idea about dat list

Richard MacCutchan 20-May-21 7:56am
   
I do not know how many times I need to say this: Do not use a RichtextBox to build all these strings.
Member 12659926 20-May-21 7:22am
   
if u have any example code with dat list kindly share

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900