System I/O and Streams in VB.NET: Part 1

In this article I will explain you about System I/O and Streams in VB.NET.
  • 2221

A software system has three major components: input, output, and processing.

Figure6.1.gif  The input part of the system is responsible for accepting data in the form of bytes, streams, binary data, or strings from input devices such as keyboards, mouses, pads, or other media. In the processing component of a software system, you apply your logic on the data to produce some information. The output part of the system displays processed data through devices such as monitors or printers.

Input/output (I/O) is an important consideration when you design a system or computer application. All software applications must control input and output in some manner.

The .NET Framework provides a rich application program interface (API) to work with the system I/O. This API resides in the System.IO namespace.

Overview of System.IO Namespace

In the .NET Framework, the System.IO namespace defines classes for reading and writing files and data streams. The System.IO namespace, which resides in the mscorlib.dll assembly, provides classes for working with the system I/O and with streams.

The main functionality of the System.IO namespaces in .NET includes the following features:

  • Accessing Buffer: The .NET Framework defines classes for reading and writing bytes, multibytes, binary data, strings, and character data with the help of StreamReader, StreamWriter, BinaryReader, BinaryWriter, StringReader, and StringWriter classes. The FileStream class can be used for random file access.
     

  • File and Directory Operations: The classes in the System.IO namespaces provide functionality for creation, deletion, and manipulation of files and directories. You can use the File, FileInfo and the Directory, DirectoryInfo classes to do most of your file and directory manipulations. The FileSystemWatcher class is used to monitor the file system.
     

  • Performance Optimization: The MemoryStream and the BufferStream classes enhance the performance of read/write operations by storing data in memory.

System I/O

The Console Class

The Console class from the System namespace helps you with system I/O to the command prompt window or console. The System.IO namespace contains classes like BinaryReader/BinaryWriter, StreamReader/StreamWriter, and FileStream to process streams of different kinds. All these classes are contained within the mscorlib.dll assembly.

The Console class provides access to the three standard streams-standard input, standard output, and standard error-by way of Console.In, Console.Out, and Console.Error, respectively.

System Output

The Console class has two methods, Write and WriteLine, to display output to the console. The distinction between these two methods is that the WriteLine method adds a line terminator after the output so you don't have to manually add the /n character to go to the next console line. Both methods are overloaded to take a variety of primitive data types.

If you are passing a class containing data other than the primitive types, then that class's ToString method is called and printed on the screen. If the class does not have an implementation of the ToString method, then the object class's ToString method is called.

Let's look at an example of overriding the ToString method.

Example of Overriding ToString()

    Public Class OneMem
            Private i As Integer = 10
        Public Class OneOver
            Private i As Integer = 10
            Public Overrides Function ToString() As String
                Return
i.ToString()
            End Function
        End
Class
        Public
Class OneUser
            Public Shared Sub Main()
                Dim F As New OneMem()
                Dim B As New OneOver()
                Console.WriteLine(F)
                'This will print "OneMem"
                Console.WriteLine(B)
                'This will print "10"
                Console.ReadLine()
            End Sub
        End
Class
    End
Class

Output of above example

  ar 3.gif

Conclusion

Hope this article would have helped you in understanding System I/O and Streams in VB.NET. Remaining Part of this article will on my next article.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.