click below
click below
Normal Size Small Size show me how
Chapter 17
Files and Streams
Term | Definition |
---|---|
Files | Used for long-term retention of large amounts of data, even after the program that created the data terminates. A group of related records |
Persistent Data | Data maintained in files |
Secondary Storage Devices | Magnetic disks, optical disks, flash memory and magnetic tapes. Computers store files on secondary storage devices |
Bits | The smallest data item that computers support. Can assume either the value 0 or the value 1 |
Characters | Digits, letters, and special symbols |
Decimal Digits | 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 |
Letters | A-Z and a-z |
Special Symbols | $, @, %, &, *, (, ), -, +, ", :, ?, / |
Character Set | The set of all characters used to write programs and represent data items on a particular computer |
Bytes | Composed of eight bits |
Fields | A group of characters that convey meaning. Just as characters are composed of bits, fields are composed of characters. Ex: Judy |
Data Hierarchy | Data items processed by computers form a data hierarchy, in which data items become larger and more complex in structure as we progress from bits to characters to fields to larger data aggregates |
Records | Composed of several related fields. For example, a record for a particular employee might include... 1. ID number... 2. Name... 3. Address... 4. Hourly Pay Rate...Each field is associated with same employee |
Record Key | Identifies a record as belonging to a particular person or entity and distinguishes that record from all others. To facilitate the retrieval of specific records from a file, at least one field in each record is chosen as a record key |
Sequential File | A common file organization in which records typically are stored in order by a record-key field |
Database | Related data are often stores in databases |
Database Management System(DBMS) | A collection of programs designed to create or manage databases |
Streams | C# views each file as a sequential stream of bytes |
End-Of-File Marker | Each file ends either with an end-of-file marker or at a specific byte number that's recorded in a system-maintained administrative data structure |
Console.Out, Console.In, Console.Error | When a console app executes, the runtime environment creates three stream objects that are accessible via properties Console.out, Console.In, Console.Error. These objects use streams to facilitate communication between a program and a 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 |
System.I0 Namespace | Includes stream classes such as StreamReader(for text input form a file), StreamWriter(for text output to a file), and FileStream(for both input and output to file). These stream classes inherit from abstract classes TextReader, TextWriter, and Stream |
Stream | Abstract class Stream provides functionality for representing streams as bytes. Classes FileStream, MemoryStream, and BufferedStream(all from namespace System.I0) 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 and writing to external devices |
Class BufferedStream | Uses buffering to transfer data to or from a stream |
Buffering | An I/O performance-enhancement technique in which each output operation is directed to a region in memory, called a buffer, that's large enough to hold the data from many output operations |
Physical Output Operation | Then actual transfer to the output device is performed in one large physical output operation each time the buffer fills |
Logical Output Operations | The output operations directed to the output buffer in memory |
Classes File and Directory | Enable programs to manipulate files and directories on disk |
Class File | Can determine information about files and can be used to open files for reading or writing |
1. File Class static Methods(partial list) | 1. Method[AppendText]..Description[Returns a StreamWriter that appends text to an existing file or creates a file if one does not exist]... 2. Method[Copy]..Description[Copies a file to a new file]... |
2. File Class static Methods(partial list) | 3. Method[Create]..Description[Creates a file and returns its associated Filestream]... 4. Method[CreateText]..Description[Creates a text file and returns its associated StreamWriter]... |
3. File Class static Methods(partial list) | 5. Method[Delete]..Description[Deletes the specified code]... 6. Method[Exists]..Description[Returns true if the specified file exists and false otherwise]... |
4. File Class static Methods(partial list) | 7. Method[GetCreationTime]..Description[Returns a DateTime object representing when the file was created]... 8. Method[GetLastAccessTime]..Description[Returns a DateTime object representing when the file was last accessed]... |
5. File Class static Methods(partial list) | 9. Method[GetLastWriteTime]..Description[Returns a DateTime object representing when the file was last modified]... 10. Method[Move]..Description[Moves the specified file to a specified location]... |
6. File Class static Methods(partial list) | 11. Method[Open]..Description[Returns a FileStream associated with the specified file and equipped with the specified read/write permissions]... 12. Method[OpenRead]..Description[Returns a read-only FileStream associated with the specified file]... |
7. File Class static Methods(partial list) | 13. Method[OpenText]..Description[Returns a StreamReader associated with the specified file]... 14. Method[OpenWrite]..Description[Returns a write FileStream associated with the specified file] |
Class Directory | Provides capabilities for manipulating directories |
1. Directory Class static Methods | 1. Method[CreateDirectory]..Description[Creates a directory and returns its associated DirectoryInfo object which contains information about a directory]... 2. Method[Delete]..Description[Deletes the specified directory]... |
2. Directory Class static Methods | 3. Method[Exists]..Description[Returns true if the specified directory exists and false otherwise]... 4. Method[GetDirectories]..Description[Returns a string array containing the names of the subdirectories in the specified directory]... |
3. Directory Class static Methods | 5. Method[GetFiles]..Description[Returns a string array containing the names of the files in the specified directory]... 6. Method[GetCreationTime]..Description[Returns a DateTime object representing when the directory was created]... |
4. Directory Class static Methods | 7. Method[GetLastAccessTime]..Description[Returns a DateTime object representing when the directory was last accessed]... 8. Method[GetLastWriteTime]..Description[Returns a DateTime object representing when items were last written to the directory]... |
5. Directory Class static Methods | 9. Method[Move]..Description[Moves the specified directory to a specified location] |
Dictionary | A Dictionary(namespace System.Collections.Generic) is a collection of key-value pairs, in which each key has a corresponding value |
Class Dictionary | A generic class |
Method ContainsKey | Dictionary method ContainsKey determines whether the specified file-name extension has been placed in the Dictionary previously |
Method Add | Dictionary method Add inserts a new key-value pair into the Dictionary |
Method ShowDialog | Returns a DialogResult specifying which button(Save or Cancel) the user clicked to close the dialog |
FileStream | You can open files to perform text manipulations by creating object of class FileStream. Ex: FileStream output = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write); |
Constant FileMode.OpenOrCreate(2nd FileStream Argument) | Indicates that the FileStream object should open the file if it exists or create the file if it does not exist |
StreamWriter | The contents of an existing file are overwritten by the StreamWriter. Ex: fileWriter = new StreamWriter(output);. To preserve the original contents of a file, use FileMode.Append |
Constant FileAccess.Write(3rd FileStream Argument) | Indicates that the program can perform only write operations with the FileStream object |
FileAccess.Read(3rd FileStream Argument) | For read-only access |
FileAccess.ReadWrite(3rd FileStream Argument) | For read and write access |
1. File-Position Pointer | A FileStream object can reposition its file-position pointer(which contais the byte number of the next byte to be read from or written to the file) to any position in the file. |
2. File-Position Pointer | When a FileStream object is opened, its file-position pointer is set to byte position 0(beginning of the file) |
Serialized Object | An object represented as a sequence of bytes that includes the object's data, as well as information about the object's type and the types of data stored in the object. |
Deserialized | After a serialized object has been written to a file, it can be read from the file and deserialized - that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory |
Class BinaryFormatter | Enables entire objects to be written to or read from a stream. Namespace System.Runtime.Serialization.Formatters.Binary |
Method Serialize | BinaryFormatter method Serialize writes an object's representation to a file |
Method Deserialize | BinaryFormatter method Deserialize reads this representation from a file and reconstructs the original object |
SerilizationException | An exception thrown by both method Deserialize and Serialize if an error occurs during serialization or deserialization. Both methods require a Stream object as a parameter so that the BinaryFormatter can access the correct stream |