70 likes | 80 Views
Learn how to organize records in a file using sequential organization. Explore the FileStream, MemoryStream, and BufferedStream classes for reading and writing data. Understand how to manipulate directories using the Directory class.
E N D
File and Streams • There are many ways of organizing records in a file. • The most common type of organization is called a sequential file. • .net framework views each file as a sequential stream of bytes • Each file ends with either an end-of-file marker or at a specific byte number
Class stream • Abstract class Stream (namespace System::IO) provides functionality for representing streams as bytes • Class FileStream, MemoryStream and BufferedStream inherit from class Stream • FileStream: read from and write data to sequential-access and random-access file • MemoryStream: enables transfers of data directly to and from memory (faster) • BufferedStream: use buffering to transfer data to or from a stream. (performance enhancement technique, in which each I/O operation is directed to a region in memory, called a buffer. The actual transfer is performed in one large physical output operation each time the buffer is filled
Class File • Class File is provided for maintaining files • Class File cannot read form or write to files directly • Separator character /. In .net, both / and \ are valid • However, when use \ in string, one must use \\ • For example, C:\\CS603/project
Static methods • Class File contain only static methods • You cannot instantiate objects of type File • Copy: copies a file to a new file • Create: creates a file and return its associated FileStream • Delete: deletes the specified file • Exists: return true if the specified file exists; otherwise, it returns false • GetCreationTime: return DateTime object representing the time that file was created • GetLastAccessTime: • GetLastWriteTime: • Move: moves the specified file to a specified location
Static methods- continued • Open: return FileStream associated with the specified file • OpenRead: returns a read-only FileStream associated with the specified file • OpenWrite: return a read/write FileStream associated with the specified file • OpenText: returns a StreamReader associated with the specified file
Class Directory • Class Directory is provided for manipulating directories. • It provides capabilities for manipulating and iteration directories • Static methods • CreateDirectory: creates a directory and returns its associated DirectoryInfo. • The DirectoryInfo object returned by method CreateDirectory contains information about a directory.
Other static methods • Delete: deletes the specified directory • Exists: return true if the specified directory exists; otherwise, it returns false • GetLastWriteTime: return a DataTime object representing the time that the directory was last modified • GetDirectories: return a String *array representing the names of the files in the specified directory • GetFiles: returns a String * array representing the names of the files in the specified directory • GetCreationTime