1 / 35

File Input/Output (I/O)

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.

mona-lucas
Download Presentation

File Input/Output (I/O)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. File Input/Output (I/O) Tonga Institute of Higher Education

  2. 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

  3. System.IO Namespace • The System.IO namespace contains many classes we can use to work with files and directories • DirectoryInfo • FileInfo • StreamReader • StreamWriter

  4. DirectoryInfo • Contains methods for working with directories.

  5. 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.

  6. 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.

  7. Demonstration DirectoryInfo

  8. FileInfo • Contains methods for working with files.

  9. 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.

  10. 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.

  11. FileInfo Collections

  12. Demonstration FileInfo

  13. 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

  14. 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.

  15. 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

  16. StreamReader 2 The end of the file is reached when line is nothing

  17. Demonstration StreamReader

  18. 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

  19. Demonstration StreamWriter

  20. 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

  21. 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.

  22. Demonstration File I/O Exceptions

  23. 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

  24. Open Dialog Box • Provides GUI for selecting a file to open

  25. Open Dialog Box GUI There are many properties • The properties can be set using the GUI

  26. Open Dialog Box GUI Code • But code must be used to display it and handle what the user does with it.

  27. Demonstration OpenFileDialog with GUI and Code

  28. 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

  29. Demonstration OpenFileDialog with Only Code

  30. Save Dialog Box • Provides GUI for inputting the name and location of a file to save

  31. Save Dialog Box GUI There are many properties • The properties can be set using the GUI

  32. Save Dialog Box GUI Code • But code must be used to display it and handle what the user does with it.

  33. Demonstration SaveFileDialog with GUI and Code

  34. 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

  35. Demonstration SaveFileDialog with Only Code

More Related