locked
I need an example of the ReadBlock method of StreamReader.

    Question

  • I couldn't find an example of the ReadBlock method of StreamReader in the help files of VB 2010.
    Monday, May 30, 2011 3:30 PM

Answers

  • The MSDN library article on StreamReader.ReadBlock is at http://msdn.microsoft.com/en-us/library/system.io.textreader.readblock.aspx.  Here is an example that reads 20 characters beginning 10 characters from the start of the file and stores the result in a string.

    Dim rdr As New IO.StreamReader(filePath)
    Dim block(20) As Char
    rdr.BaseStream.Seek(10, IO.SeekOrigin.Begin)
    Dim numChars As Integer = rdr.ReadBlock(block, 0, block.Length)
    Dim blockString As String = New String(block)
    
    

    • Marked as answer by ponyplayer Monday, May 30, 2011 4:43 PM
    Monday, May 30, 2011 4:31 PM

All replies

  • I couldn't find an example of the ReadBlock method of StreamReader in the help files of VB 2010.

    Hello ponyplayer,

    I hope you understand what you need, here are some examples of
    Using StreamReader

    http://msdn.microsoft.com/en-us/library/system.io.streamreader(v=VS.80).aspx

    Imports System
    Imports System.IO
    
    Class Test
    
     Public Shared Sub Main()
    
    
    
     Try
    
     ' Create an instance of StreamReader to read from a file.
    
     Dim sr As StreamReader = New StreamReader("TestFile.txt")
    
     Dim line As String
    
     ' Read and display the lines from the file until the end 
    
     ' of the file is reached.
    
     Do
    
     line = sr.ReadLine()
    
     Console.WriteLine(Line)
    
     Loop Until line Is Nothing
    
     sr.Close()
    
     Catch E As Exception
    
     ' Let the user know what went wrong.
    
     Console.WriteLine("The file could not be read:")
    
     Console.WriteLine(E.Message)
    
     End Try
    
     End Sub
    
    End Class
    
    
    
    

    Hello

     


    Carmelo La Monica  http://community.visual-basic.it/carmelolamonica/

     

     



    Monday, May 30, 2011 3:40 PM
  • Thanks for the reply. I can read an entire line and I can read one item in a line but I can't seem to work out how to read a specific size block of a text file.
    Monday, May 30, 2011 4:04 PM
  • The MSDN library article on StreamReader.ReadBlock is at http://msdn.microsoft.com/en-us/library/system.io.textreader.readblock.aspx.  Here is an example that reads 20 characters beginning 10 characters from the start of the file and stores the result in a string.

    Dim rdr As New IO.StreamReader(filePath)
    Dim block(20) As Char
    rdr.BaseStream.Seek(10, IO.SeekOrigin.Begin)
    Dim numChars As Integer = rdr.ReadBlock(block, 0, block.Length)
    Dim blockString As String = New String(block)
    
    

    • Marked as answer by ponyplayer Monday, May 30, 2011 4:43 PM
    Monday, May 30, 2011 4:31 PM
  • Perfect!!! That solves my problem. I was close but couldn't quite get the proper syntax. Thanks a bunch.
    Monday, May 30, 2011 4:45 PM
  • Hi Blackwood

    I want Split blockString using NewLine('\n') and my block size is 4096 I did this functionality but some time I got Incomplete line so that how can use this incomplete line in next block so that I can get complete line using next block

     

    Thanks-

    Atul Katare

    Monday, November 21, 2011 9:00 AM
  • Sorry Atul, I don't always check threads that are marked as answered, so I only just noticed you question.

    When you split the block you have read, the incomplete line will be the last element of the array.  If the block happened to end with a newline character, the last element will be an empty string.  I suggest you store the incomplete line in a variable, and when you read the next block, concatenate the fragment you have saved with the first element of the array you create from the next block.

    The following example code (which I have not tested) shows how I would do that.

    Dim block(4096) As Char, lines() As String 
    Dim fragment As String = ""
    		
    Using rdr As New IO.StreamReader(filePath)
    	Do Until rdr.EndOfStream
    		Dim numChars As Integer = rdr.ReadBlock(block, 0, block.Length)
    		Dim blockString As String = New String(block)
    		lines = blockString.Split(CChar(vbLf))
    		lines(0) = fragment & lines(0)
    		fragment = lines(lines.Length - 1)
    				
    		For Each line As String In lines
    			'process each line
    		Next
    	Loop
    End Using
    		
    If fragment <> "" Then 'last line did not end with \n - it will be stored in fragment
    	'process fragment
    End If
    
    


    Tuesday, November 29, 2011 2:30 PM