Chapter 10. Streams, Files, and I/O

This chapter discusses the facilities the Microsoft .NET Framework provides for input/output. Most programs require the ability to store persistent data in the file system or to communicate with another process. With one exception, the topics discussed in this chapter are familiar to the Java programmer. Concepts such as streams, working with the file system, serialization, and using the console have been staple elements of Java for some time. The only aspect of the .NET I/O support that will be new is known as isolated storage, which allows data to be easily stored on a per-user basis.

Working with the Console

Reading from and writing to the console are two of the most fundamental uses of I/O, especially for server-side applications. Despite improvements in logging and debugging tools, writing to the console is still a prevalent means of tracking activity and problems in code. During development we still find console output to be the quickest and simplest means of getting a handle on a tricky problem. Reading from the console is less common but is still an important tool. A good UI goes a long way, but sometimes there is no substitute for grabbing a keystroke straight from the user.

In Java, the console is exposed to the programmer as a set of streams obtained through the java.lang.System class. The .NET Framework puts all of the console functions in the System.Console class. The Console class is a wrapper around three streams, which are accessible via read-only properties. These are shown alongside their Java equivalents in Table 10-1.

Table 10-1. Comparison Between the Java and .NET Standard Streams

Java

.NET

Comments

System.out

Console.Out

The standard output stream

System.in

Console.In

The standard input stream

System.err

Console.Error

The standard error output stream

The Error and Out properties return System.IO.TextWriter streams, whereas the In property returns an instance of System.IO.TextReader. These classes are covered in more detail later in this chapter in the Readers and Writers section, but for now it’s enough to know that they provide much the same support as the streams available in the java.lang.System class.

Writing to the Console

The System.IO.TextWriter class returned by the Out and Error properties is similar in functionality to the java.io.PrintStream class that is used for the System.out and System.err streams in Java. The TextWriter.Write method is overloaded to accept a wide range of simple types and formatted strings for display on the console. The WriteLine method offers the same overloads but appends a line terminator to the output. By default, the line terminator is the sequence, but it can be changed using the TextWriter.NewLine property.

The Console class also provides two convenience methods that offer the same overloads and write to the Out stream, making the following pairs of statements equivalent:

Console.Out.WriteLine("This is a message");
Console.WriteLine("This is a message");

Console.Out.Write("This is a message");
Console.Write("This is a message");

The Console class doesn’t provide these convenience methods for the standard error stream, so error messages must be written using the Console.Error.Write and Console.Error.WriteLine methods directly.

More Info

All Write and WriteLine methods discussed in this section offer overloads that will generate formatted strings as discussed in Chapter 7.

Reading from the Console

Input can be read from the console either through the convenience methods in the Console class or through the System.IO.TextReader instance accessible via the Console.In property. In keeping with the model for writing to the console, the following pairs of statements are equivalent:

Console.In.ReadLine();
Console.ReadLine();

Console.In.Read();
Console.Read();

The ReadLine method reads the next line of characters from the input stream, stripping out the line terminator. The Read method returns a single character of input but will do so only after a read operation has been terminated, typically because the user has pressed the Enter key. Java provides the ability to read a complete line from the standard input by wrapping System.in in either a java.io.BufferedReader or a java.io.DataInputStream, but it doesn’t provide this feature directly in the console stream.

Changing the Console Streams

In common with Java, the .NET console class allows new streams to be substituted for the default instances. This is useful for redirecting console output to a file or filtering the output before displaying it to the user. The In, Out, and Error properties are read-only, but the SetIn, SetOut, and SetError methods of Console can be used to install new instances of TextReader and TextWriter as required.

Console Summary

Table 10-2 provides a summary of the Java-to-.NET mappings for using the console. All members are static.

Table 10-2. Comparison Between the Java and .NET Console Classes

Java

.NET

System.in

Console.In

System.out

Console.Out

System.err

Console.Error

System.out.println()

Console.WriteLine()

 

Console.Out.WriteLine()

System.out.print()

Console.Write()

 

Console.Out.Write()

System.in.read()

Console.Read()

 

Console.In.Read()

N/A

Console.ReadLine()

 

Console.In.ReadLine()

System.err.print()

Console.Error.Write()

System.err.println()

Console.Error.WrintLine()

System.setIn()

Console.SetIn()

System.setOut()

Console.SetOut()

System.setErr()

Console.SetError()

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset