0% found this document useful (0 votes)
4 views

complete-reference-vb_net_59

Uploaded by

khalid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

complete-reference-vb_net_59

Uploaded by

khalid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

BufferedStream

'Position the file StreamReader file pointer at the beginning.


wordReader.BaseStream.Seek(0, SeekOrigin.Begin)

The following example demonstrates the Current constant of the SeekOrigin enumeration. First it creates a
FileStream object with the file access option set to write. Then it creates a StreamWriter object to write the
data into the file (with full path) passed to the method's source parameter. The word to write to the file is
passed to the noiseword parameter.

Public Sub AddNoises(ByVal source As String, _


ByVal noiseword As String)
Dim fStream As New FileStream(source, FileMode.OpenOrCreate, _
FileAccess.Write)
' Create a 'StreamWriter' to write the data into the file.
Dim sWriter As New StreamWriter(fStream)
sWriter.WriteLine(noiseword)
' Update the 'StreamWriter'.
sWriter.Flush()
' Close the 'StreamWriter' and FileStream.
sWriter.Close()
fStream.Close()
End Sub

The following method now reads the contents of the file into an array as follows:

Public Sub ReadNoises(ByVal source As String)


Dim fStream As New FileStream(source, FileMode.OpenOrCreate, _
FileAccess.Read)
'Place the cursor at the beginnig of the file.
fStream.Seek(0, SeekOrigin.Begin)
'Get a byte array
Dim byteArray(20) As Byte
'Read the first twenty characters into the byte array.
fStream.Read(byteArray, 0, 20)
Dim Encoder As New ASCIIEncoding()
Console.WriteLine("The Contents of the array are {0}: ", _
Encoder.GetString(byteArray))
Console.WriteLine(Encoder.GetString(byteArray))
'Increment the file pointer from CurrentPosition by one character.
fStream.Seek(1, SeekOrigin.Current)
'Read the next five characters.
fStream.Read(byteArray, 0, 20)
Console.WriteLine("The rest of the array are {0}: ", _
Encoder.GetString(byteArray))
'Close the FileStream.
fStream.Close()
End Sub

BufferedStream

A buffered stream's purpose in life is to read and write to another stream. BufferedStream is essentially a
block of bytes in memory used to cache data, thereby reducing the number of calls to the operating system.
You will use your buffers to improve read and write performance, but you cannot use a buffer to read and
write at the same time. Objects of the BufferedStream work like the FileStream object, but the Read and
Write methods of BufferedStream are used to automatically maintain the buffer. I liken it to overdraft
protection for a stream.

543

You might also like