Kurt Nørmark ©
Department of Computer Science, Aalborg University, Denmark
Abstract Previous lecture Next lecture Index References Contents | This is the lecture about classes for input and output in C#. The most fundamental of these are class Stream and its subclasses. In addition to the Stream classes, C# also supports a number of so-called Reader and Writer classes. These classes are built on (delegate to) a stream, but they do not inherit from a stream. We discuss Text and Binary readers and writers. We also look at string readers and string writers. In addition, we will study the classes that allows us to represent files and directories as objects. Text/char encoding, the Console class, and C# attributes are also touched on. We devote a part of this lecture to a study of serialization, which is a simple and easy means to obtain persist objects. The lecture is finalized with a discussion of the Decorator design pattern, which is relevant for building one stream on top of another stream. |
Streams |
The Stream Concept Slide Annotated slide Contents Index References Textbook |
The concept stream: A stream is a flow of data from a program to a backing store, or from a backing store to a program The program can either write to a stream, or read from a stream. |
Reading from and writing to a stream |
|
The abstract class Stream in C# Slide Annotated slide Contents Index References Textbook |
|
|
The most important members in class Stream Slide Annotated slide Contents Index References Textbook |
|
|
|
Subclasses of class Stream Slide Annotated slide Contents Index References Textbook |
|
|
Example: Filestreams Slide Annotated slide Contents Index References Textbook |
|
|
Program: A program that writes bytes corresponding to 'O' 'O' 'P' to a file stream. |
|
Program: The contents of myFile.bin - interpreted as simple ASCII text - after executing the write program. |
|
Program: A program that reads the written file. |
|
Program: Console output from the read program. |
|
|
The using control structure Slide Annotated slide Contents Index References Textbook |
|
Syntax: The syntax of the using statement C# |
|
|
|
Program: The control structure 'using' defined by 'try-finally'. |
|
Program: The simple write-program programmed with 'using'. |
|
Program: The simple read-program programmed with 'using'. |
|
More FileStream Examples Slide Annotated slide Contents Index References Textbook |
|
|
Program: A FileCopy method in a source file copy-file.cs - uses two FileStreams. |
|
Program: A sample activation of the file copy program. |
|
Exercise 10.2. A variant of the file copy program | The purpose of this exercise is to train the use of the Read method in class Stream, and subclasses of class Stream. Write a variant of the file copy program. Your program should copy the entire file into a byte array. Instead of the method ReadByte you should use the Read method, which reads a number of bytes into a byte array. (Please take a careful look at the documentation of Read in class FileStream before you proceed). After this, write out the byte array to standard output such that you can assure yourself that the file is read correctly. Are you able to read the entire file with a single call to Read? Or do you prefer to read chunks of a certain (maximum) size? |
Readers and Writers in C# Slide Annotated slide Contents Index References Textbook |
|
Table. An overview of Reader and Writer classes |
|
|
The class Encoding Slide Annotated slide Contents Index References Textbook |
|
|
|
Sample use of class Encoding Slide Annotated slide Contents Index References Textbook |
Program: Sample encodings, conversions, and decodings of a string of Danish characters. |
|
Program: Output from the Encoding program. |
|
|
Exercise 10.3. Finding the encoding of a given text file | Make a UTF-8 text file with some words in Danish. Be sure to use plenty of special Danish characters. You may consider to write a simple C# program to create the file. You may also create the text file in another way. In this exercise you should avoid writing a byte order mark (BOM) in your UTF-8 text file. (A BOM in the UTF-8 text file may short circuit the decoding we are asking for later in the exercise). One way to avoid the BOM is to denote the UTF-8 encoding with new UTF8Encoding(), or equivalently new UTF8Encoding(false). You may want to consult the constructors in class UFT8Encoding for more information. Now write a C# program which systematically - in a loop - reads the text file six times with the following objects of type Encoding: ISO-8859-1, UTF-7, UTF-8, UTF-16 (Unicode), UTF32, and 7 bits ASCII. More concretely, I suggest you make a list of six encoding objects. For each encoding, open a TextReader and read the entire file (with ReadToEnd, for instance) with the current encoding. Echo the characters, which you read, to standard output. You should be able to recognize the correct, matching encoding (UTF-8) when you see it. |
The class TextWriter Slide Annotated slide Contents Index References Textbook |
|
|
|
StreamWriter Examples Slide Annotated slide Contents Index References Textbook |
|
Program: Writing a text string using three different encodings with StreamWriters. |
|
|
Program: Writing values of simple types and objects of our own classes. |
|
Program: The file simple-types.txt. |
|
Program: The file non-simple-types.txt. |
|
Exercise 10.4. Die tossing - writing to text file | Write a program that tosses a Die 1000 times, and writes the outcome of the tosses to a textfile. Use a TextWriter to accomplish the task. Write another program that reads the text file. Report the number of ones, twos, threes, fours, fives, and sixes. |
|
Members in class StreamWriter Slide Annotated slide Contents Index References Textbook |
|
The class TextReader Slide Annotated slide Contents Index References Textbook |
|
|
|
StreamReader Examples Slide Annotated slide Contents Index References Textbook |
|
|
Program: Reading back the text strings encoded in three different ways, with StreamReader.
In the last half part of the program, the binary contents of the three files are read and reported.
|
|
Program: Output from the program that reads back the strings encoded in three different ways. |
|
|
Program: A program that reads line of text and parses them to values of simple types. |
|
Program: Output from the readline and parsing program. |
|
|
Members in class StreamReader Slide Annotated slide Contents Index References Textbook |
|
The class BinaryWriter Slide Annotated slide Contents Index References Textbook |
|
|
Program: Use of a BinaryWriter to write some values of simple types. |
|
Program: Output from the BinaryWriter program - shows size of output file. |
|
|
Exercise 10.5. Die tossing - writing to a binary file | This exercise is a variant of the die tossing and file writing exercise based on text files. Modify the program to use a BinaryWriter and a BinaryReader. Take notice of the different sizes of the text file from the previous exercise and the binary file from this exercise. Explain your observations. |
BinaryWriter Members Slide Annotated slide Contents Index References Textbook |
|
The class BinaryReader Slide Annotated slide Contents Index References Textbook |
|
|
Program: Use of a BinaryReader to write the values written by means of the BinaryWriter. |
|
Program: Output from the BinaryReader program. |
|
Members in class BinaryReader Slide Annotated slide Contents Index References Textbook |
|
The classes StringReader and StringWriter Slide Annotated slide Contents Index References Textbook |
|
Table. An overview of Reader and Writer classes with special emphasis on StringReader and StringWriter. |
|
Program: A StringWriter program similar to the StreamReader program shown earlier. |
|
Program: Output of the StringWriter program. |
|
Program: A StringReader program. |
|
Program: Output of the StringReader program. |
|
The Console class Slide Annotated slide Contents Index References Textbook |
|
Program: A program that redirects standard output and standard error to a file. |
|
Program: Output when running the program as App File1 File2. |
|
Program: File 1 after the execution of the Console demo program. |
|
Program: File 2 after the execution of the Console demo program. |
|
Program: Output when running the program as just App. |
|
Members in the Console class Slide Annotated slide Contents Index References Textbook |
|
|
Directories and Files |
The File and FileInfo classes Slide Annotated slide Contents Index References Textbook |
|
Program: A demonstration of the FileInfo class. |
|
Program: Output from the FileInfo demo program. |
|
Program: A demonstration of the File class. |
|
Program: Output from the File demo program. |
|
|
Members in class FileInfo Slide Annotated slide Contents Index References Textbook |
|
|
The Directory and DirectoryInfo classes Slide Annotated slide Contents Index References Textbook |
|
Program: A demonstration of the DirectoryInfo class. |
|
Program: Output from the DirectoryInfo demo program. |
|
Program: A demonstration of the Directory class - similar to the DirectoryInfo demo program. |
|
Program: Output from the Directory demo program. |
|
|
Members in class DirectoryInfo Slide Annotated slide Contents Index References Textbook |
|
|
|
Serialization |
Serialization Slide Annotated slide Contents Index References Textbook |
|
|
|
Examples of Serialization in C# Slide Annotated slide Contents Index References Textbook |
|
Program: The Person class - Serializable. |
|
Program: The Date class - Serializable. |
|
Program: The Person client class - applies serialization and deserialization. |
|
Program: Output of the Person client class. |
|
Exercise 10.7. Serializing one of your own classes | The purpose of this exercise is to let you have your first experiences with serialization of some of your own object, which are instances of your own classes. Select one or more of your own classes, typically from the project on which you are working. Mark the classes as Serializable, in the same way as we have done in class Person and class Date on the accompanying slide page. Consider if some of the instance variables should be marked as NonSerialized and if an OnDeserialized method should be provided. Like in the client program of class Person and class Date, make a sample client class of your own classes, and call the Serialize and the Deserialize methods. |
Exercise 10.7. Serializing with an XML formatter | In the programs shown on the accompanying slide we have used a binary formatter for serialization of Person and Date object. Modify the client program to use a so-called Soap formatter in the namespace System.Runtime.Serialization.Formatters.Soap. SOAP is an XML language intended for exchange of XML documents. SOAP is related to the discipline of web services in the area of Internet technology. After the serialization you should take a look at the file person.dat, which is written and read by the client program. |
Custom Serialization Slide Annotated slide Contents Index References Textbook |
|
Program: The Person class - Serialization control with attributes. |
|
Program: The Date class - Serialization control with attributes . |
|
Program: The Person client class - applies serialization. Unchanged. |
|
Program: Output of the Person client class. |
|
Considerations about Serialization Slide Annotated slide Contents Index References Textbook |
|
Serialization and Alternatives Slide Annotated slide Contents Index References Textbook |
|
|
Attributes Slide Annotated slide Contents Index References Textbook |
|
Program: An obsolete class C, and a class D with an obsolete method M. |
|
Program: Compiling class C, D, and E. |
|
|
Program: A reproduction of class ObsoleteAttribute. |
|
Program: Sample usage of the reproduced class - causes a compilation error. |
|
Patterns and Techniques |
The Decorator Pattern Slide Annotated slide Contents Index References Textbook |
|
|
A template of the class structure in the Decorator design pattern. |
|
The Decorator Pattern at Run Time Slide Annotated slide Contents Index References Textbook |
Two decorator objects of a ConcreteComponent object |
|
|
The Decorator Pattern and Streams Slide Annotated slide Contents Index References Textbook |
|
Compression and buffering decoration of a FileStream |
Program: A program that compresses a file. |
|
Program: Sample application together with program output (compression rate). |
|
Program: The corresponding program that decompresses the file. |
|
Chapter 10: Input and Output Classes
Course home Author home About producing this web Previous lecture (top) Next lecture (top) Previous lecture (bund) Next lecture (bund)
Generated: February 7, 2011, 12:19:26