350 likes | 472 Views
File Input/Output (I/O). Tonga Institute of Higher Education. Working with Files and Directories. Programs often work with files Changing files or directories Getting information about file or directory Getting data form a file or directory Saving data in a file or directory.
E N D
File Input/Output (I/O) Tonga Institute of Higher Education
Working with Files and Directories • Programs often work with files • Changing files or directories • Getting information about file or directory • Getting data form a file or directory • Saving data in a file or directory
System.IO Namespace • The System.IO namespace contains many classes we can use to work with files and directories • DirectoryInfo • FileInfo • StreamReader • StreamWriter
DirectoryInfo • Contains methods for working with directories.
DirectoryInfo Properties • Exists - Gets a value indicating whether the directory exists. • FullName - Gets the full path of the directory or file. • Name - Gets the name of this DirectoryInfo instance. • Parent - Gets the parent directory of a specified subdirectory. • Root - Gets the root portion of a path.
DirectoryInfo Methods • Create - Creates a directory • CreateSubdirectory - Creates a subdirectory or subdirectories on the specified path. • Delete - Deletes a DirectoryInfo and its contents from a path. • GetDirectories - Returns the subdirectories of the current directory. • GetFiles - Retrieves an array of FileInfo objects. • MoveTo - Moves a DirectoryInfo instance and its contents to a new path. • Refresh - Refreshes the state of the object. • ToString - Returns the original path that was passed by the user.
Demonstration DirectoryInfo
FileInfo • Contains methods for working with files.
FileInfo Properties • Attributes - Gets or sets the FileAttributes of the current FileSystemInfo. • Directory - Gets an instance of the parent directory • DirectoryName - Gets a string representing the directory's full path. • Exists - Gets a value indicating whether a file exists. • Extension - Gets the string representing the extension part of the file. • FullName - Gets the full path of the directory or file • Length - Gets the size of the current file. • Name - Gets the name of the file.
FileInfo Methods • AppendText - Creates a StreamWriter that appends text to the file represented by this instance of the FileInfo. • CopyTo - Copies an existing file to a new file. • Create - Creates a file. • CreateText - Creates a StreamWriter that writes a new text file. • Delete - Permanently deletes a file. • MoveTo - Moves a specified file to a new location, providing the option to specify a new file name. • Open - Opens a file with various read/write and sharing privileges. • OpenText - Creates a StreamReader with UTF8 encoding that reads from an existing text file. • Refresh - Refreshes the state of the object. • ToString - Returns the fully qualified path as a string.
Demonstration FileInfo
Data Source Destination What is a Stream? • A stream is a sequence of data • Streams are used to move data from one place to another • In our computer: • You can read from streams. Reading is the transfer of data from a stream into a data structure, such as a variable. • You can write to streams. Writing is the transfer of data from a data structure, such as a variable, into a stream. Stream
Writing and Reading Streams • StreamReader – Object allows program to read data from a file using a stream. • StreamWriter – Object allows program to write data to a file using a stream.
StreamReader • Allows a program to read characters from a file Name of the file that will be read Loads 1 line Always close the file. Close releases any resources used by the system
StreamReader 2 The end of the file is reached when line is nothing
Demonstration StreamReader
StreamWriter • Allows a program to write characters to a file Name of the file that will be written WriteLine includes a carriage return Close releases any resources used by the system
Demonstration StreamWriter
File I/O Exceptions • Often, errors occur when file I/O statements are used. • The name of the file is changed • A directory is deleted • The location of a file is incorrect • File I/O exceptions allow the program to handle certain errors. Causes an error Handles situation when file not found Handles other situations
Common File I/O Exceptions • DirectoryNotFoundException - The exception that is thrown when part of a file or directory cannot be found. • FileNotFoundException The exception that is thrown when an attempt to access a file that does not exist on disk fails. • EndOfStreamException - The exception that is thrown when reading is attempted past the end of a stream. • PathTooLongException The exception that is thrown when a pathname or filename is longer than the system-defined maximum length. • SecurityException - The caller does not have the required permission.
Demonstration File I/O Exceptions
File Dialog Boxes • Often, a user needs a GUI to select a file to open or enter the name and location of a file to save • OpenDialogBox - Provides GUI for selecting a file to open • SaveDialogBox - Provides GUI for inputting the name and location of a file to save
Open Dialog Box • Provides GUI for selecting a file to open
Open Dialog Box GUI There are many properties • The properties can be set using the GUI
Open Dialog Box GUI Code • But code must be used to display it and handle what the user does with it.
Demonstration OpenFileDialog with GUI and Code
Using OpenFileDialog with Only Code • Many developers prefer to create source code when using the OpenFileDialog box • This allows the developer to see everything they have done
Demonstration OpenFileDialog with Only Code
Save Dialog Box • Provides GUI for inputting the name and location of a file to save
Save Dialog Box GUI There are many properties • The properties can be set using the GUI
Save Dialog Box GUI Code • But code must be used to display it and handle what the user does with it.
Demonstration SaveFileDialog with GUI and Code
Using SaveFileDialog with Only Code • Many developers prefer to create source code when using the OpenFileDialog box • This allows the developer to see everything they have done
Demonstration SaveFileDialog with Only Code