Dialog

ReadFile

Jan 14th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.14 KB | None | 0 0
  1. ''' <summary>
  2.     ''' Reads a file into chunks
  3.     ''' </summary>
  4.     ''' <param name="fileName">The name for the file to read</param>
  5.     ''' <param name="bufferLength">The length for the buffer to use; indicates how large the chunks read will be</param>
  6.     ''' <param name="encoding">Encoding to use to read the file</param>
  7.     ''' <returns>An IEnumerable of string. Every element contains one chunk of data read, equalling the buffer length in size</returns>
  8.     Private Shared Function ReadFile(ByVal fileName As String, ByVal bufferLength As Int32, ByVal encoding As Encoding) As IEnumerable(Of String)
  9.         Dim result = New List(Of String)
  10.  
  11.         Using fileStream = File.OpenRead(fileName)
  12.             Const offset = 0
  13.  
  14.             Dim buffer(bufferLength) As Byte
  15.             Dim bytesRead As Int32
  16.  
  17.             While (True)
  18.                 bytesRead = fileStream.Read(buffer, offset, buffer.Length)
  19.  
  20.                 If bytesRead = 0 Then
  21.                     Exit While
  22.                 End If
  23.  
  24.                 result.Add(encoding.GetString(buffer))
  25.             End While
  26.         End Using
  27.  
  28.         Return result
  29.     End Function
Advertisement
Add Comment
Please, Sign In to add comment