Imput & Output.docx - Input & output last modified July 6,...

This preview shows page 1 - 4 out of 12 pages.

The preview shows page 2 - 4 out of 12 pages.
Input & outputlast modified July 6, 2020This chapter is dedicated to input & output in Visual Basic. Theinput & output in Visual Basic is based on streams.Streams are objects to work with input & output. A stream is anabstraction of a sequence of bytes, such as a file, an input/outputdevice, an inter-process communication pipe, or a TCP/IP socket.In Visual Basic, we have aStreamclass that is an abstract class forall streams. There are additional classes that derive from theStream class and make the programming a lot easier.MemoryStreamAMemoryStreamis a stream which works with data in a computermemory.Option Strict OnImports System.IOModule ExampleSub Main()Dim ms As Stream = New MemoryStream(6)ms.WriteByte(9)ms.WriteByte(11)ms.WriteByte(6)ms.WriteByte(8)ms.WriteByte(3)ms.WriteByte(7)ms.Position = 0Dim rs As Integerrs = ms.ReadByte()Do While rs <> -1Console.WriteLine(rs)rs = ms.ReadByte()Loop
ms.Close()End SubEnd ModuleWe write six numbers to a memory with aMemoryStream. Then weread those numbers and print them to the console.Dim ms As Stream = New MemoryStream(6)The line creates and initializes aMemoryStreamobject with a capacityof six bytes.ms.Position = 0We set the position of the cursor in the stream to the beginningusing thePositionproperty.ms.WriteByte(9)ms.WriteByte(11)ms.WriteByte(6)...TheWriteByte()method writes a byte to the current stream at thecurrent position.Do While rs <> -1Console.WriteLine(rs)rs = ms.ReadByte()LoopHere we read all bytes from the stream and print them to theconsole.ms.Close()Finally, we close the stream.$ ./memory.exe9116837Output of the example.
StreamReader & StreamWriterStreamReaderreads characters from a byte stream. It defaults toUTF-8 encoding.StreamWriterwrites characters to a stream in aparticular encoding.Option Strict OnImports System.IOModule ExampleSub Main()Dim file As Stringfile = "languages"TryDim stream As StreamReaderstream = New StreamReader(file)Console.WriteLine(stream.ReadToEnd())Catch e As IOExceptionConsole.WriteLine("Cannot read file.")End TryEnd SubEnd ModuleWe have a file called languages. We read characters from that fileand print them to the console.Dim stream As StreamReaderstream = New StreamReader(file)TheStreamReadertakes a file name as a parameter.Console.WriteLine(stream.ReadToEnd())TheReadToEnd()method reads all characters to the end of thestream.

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 12 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Two
Professor
N/A
Tags
Object Oriented Programming, Subroutine, Computer file

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture