In this article you will learn:
- To create, read, write and update files.
- The C# streams class hierarchy.
- To use classes File and Directory to obtain information about files and directories on your computer.
- To become familiar with sequential-access file processing.
- To use classes FileStream, StreamReader and StreamWriter to read text from and write text to files.
- To use classes FileStream and BinaryFormatter to read objects from and write objects to files.
- The smallest data item that computers support is called a bit (short for "binary digit" a digit that can assume one of two values)
- Because computers can process only 0s and 1s, every character in a computer’s character set is represented as a pattern of 0s and 1s Bytes are composed of eight bits
- collection of programs designed to create and manage databases is called a database management system (DBMS)
Files and Streams:
- C# views each file as a sequential stream of bytes
- Each file ends either with an end-of-file marker or at a specific byte number that is recorded in a system-maintained administrative data structure. When a file is opened, an object is created and a stream is associated with the object. When a program executes, the runtime environment creates three stream objects that are accessible via properties Console.Out, Console.In and Console.Error, respectively. These objects facilitate communication between a program and a particular file or device. Console.In refers to the standard input stream object, which enables a program to input data from the keyboard. Console.Out refers to the standard output stream object, which enables a program to output data to the screen. Console.Error refers to the standard error stream object, which enables a program to output error messages to the screen. We have been using Console.Out and Console.In in our console applications Console methods Write and WriteLine use Console.Out to perform output, and Console methods Read and ReadLine use Console.In to perform input
- There are many file-processing classes in the FCL. The System.IO namespace includes stream classes such as StreamReader (for text input from a file), StreamWriter (for text output to a file) and FileStream (for both input from and output to a file). These stream classes inherit from abstract classes TextReader, TextWriter and Stream, respectively. Actually, properties Console.In and Console.Out are of type TexTReader and TextWriter, respectively. The system creates objects of Textreader and TextWriter derived classes to initialize Console properties Console.In and Console.Out
- Abstract class Stream provides functionality for representing streams as bytes. Classes FileStream, MemoryStream and BufferedStream (all from namespace System.IO) inherit from class Stream. Class FileStream can be used to write data to and read data from files. Class MemoryStream enables the transfer of data directly to and from memory this is much faster than reading from and writing to external devices. Class BufferedStream uses buffering to transfer data to or from a stream. Buffering is an I/O performance enhancement technique, in which each output operation is directed to a region in memory, called a buffer that is large enough to hold the data from many output operations. Then actual transfer to the output device is performed in one large physical output operation each time the buffer fills. The output operations directed to the output buffer in memory often are called logical output operations. Buffering can also be used to speed input operations by initially reading more data than is required into a buffer, so subsequent reads get data from memory rather than an external device
- File Class static methods:
Copies a file to a new file.
Creates a file and returns its associated FileStream.
Creates a text file and returns its associated StreamWriter.
Deletes the specified file.
Returns true if the specified file exists and false otherwise.
Returns a DateTime object representing when the file was created.
Returns a DateTime object representing when the file was last accessed.
Returns a DateTime object representing when the file was last modified.
Moves the specified file to a specified location.
Returns a FileStream associated with the specified file and equipped with the specified read/write permissions.
Returns a read-only FileStream associated with the specified file.
Returns a StreamReader associated with the specified file.
Returns a read/write FileStream associated with the specified file.
- Directory Class static Methods:
Creates a directory and returns its associated DirectoryInfo object.
Deletes the specified directory.
Returns true if the specified directory exists and false otherwise.
Returns a string array containing the names of the subdirectories in the specified directory.
Returns a string array containing the names of the files in the specified directory.
Returns a DateTime object representing when the directory was created.
Returns a DateTime object representing when the directory was last accessed.
Returns a DateTime object representing when items were last written to the directory.
Moves the specified directory to a specified location.
- Keys.Enter: enumerator
Information on computers is stored in files, which are organized in directories. Classes File and Directory enable programs to manipulate files and directories on disk