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

complete-reference-vb_net_64

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)
5 views

complete-reference-vb_net_64

Uploaded by

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

StringReader/StringWriter

'Create a StringBuilder with 20 characters capacity and capped at 20


Dim sBuilder As New StringBuilder(20, 20)
'Create a character array to hold characters that will be
'fed into the StringBuilder.
Dim charArray As Char() = {"I"c, " "c, "l"c, "o"c, "v"c, "e"c, _
" "c, "V"c, "B"c, " "c, "."c, "N"c, "E"c, "T"c, "."c}
'Create a StringWriter...
Dim strWriter As New StringWriter()
'and bridge it to the StringBuilder object
sBuilder = strWriter.GetStringBuilder()
'Write a bunch of characters from the array to the StringBuilder.
strWriter.Write(charArray, 0, 15)
Console.WriteLine(sBuilder)
End Sub

The code writes "I Love VB .NET" to the console. The technique shown here is useful for building Strings
that you can then simply write to a text output stream. The receiver picks up the object and simply writes to
the console or similar device.

Flush

Flush can also be used to write to the output device. But the method clears the writer's buffer in the process.
The following example demonstrates flushing to a StringBuilder object:

sBuilder = strWriter.GetStringBuilder()
'Write a bunch of characters from the array to the StringBuilder.
strWriter.Write(charArray, 0, 15)
strWriter.Flush(charArray, 0, 15)
Console.WriteLine(sBuilder)

Close

The Close method simply shuts down the writer and its underlying stream as follows:

strWriter.Close()

StreamReader/StreamWriter

While derivatives of the Stream class are intended for Byte I/O, the StreamReader and StreamWriter
classes are intended for writing to and reading from a standard text file. The StreamReader and
StreamWriter classes default to UTF−8 encoding unless specified otherwise, instead of defaulting to the
ANSI code page for the current system. UTF−8 handles Unicode characters correctly and provides consistent
results on localized versions of the operating system. See the earlier "Text Encoding" discussion.

Table 15−29 lists the members of the StreamReader class; Table 15−30 lists the members of the
StreamWriter class.

The Read and Write methods read and write the number of characters specified by their Count parameter.
These are to be distinguished from BufferedStream.Read and BufferedStream.Write, which read and write
the number of bytes specified by a count parameter. Use the BufferedStream methods only for reading and
writing an integral number of byte array elements. For example:

Dim sReader As New StreamReader(FilePath)

548

You might also like