file input/output (i/o)

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

Upload: mona-lucas

Post on 15-Mar-2016

73 views

Category:

Documents


5 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: File Input/Output (I/O)

File Input/Output (I/O)

Tonga Institute of Higher Education

Page 2: File Input/Output (I/O)

Working with Files and Directories

Programs often work with filesChanging files or directoriesGetting information about file or directoryGetting data form a file or directorySaving data in a file or directory

Page 3: File Input/Output (I/O)

System.IO Namespace

The System.IO namespace contains many classes we can use to work with files and directoriesDirectoryInfoFileInfoStreamReaderStreamWriter

Page 4: File Input/Output (I/O)

DirectoryInfo Contains methods for working with directories.

Page 5: File Input/Output (I/O)

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.

Page 6: File Input/Output (I/O)

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.

Page 7: File Input/Output (I/O)

Demonstration

DirectoryInfo

Page 8: File Input/Output (I/O)

FileInfo Contains methods for working with files.

Page 9: File Input/Output (I/O)

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.

Page 10: File Input/Output (I/O)

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.

Page 11: File Input/Output (I/O)

FileInfo Collections

Page 12: File Input/Output (I/O)

Demonstration

FileInfo

Page 13: File Input/Output (I/O)

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.

Data SourceDestinationStream

Page 14: File Input/Output (I/O)

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.

Page 15: File Input/Output (I/O)

StreamReader Allows a program to read characters from a file

Name of the filethat will be read

Loads 1 line

Always close the file. Close releases any resources used by the system

Page 16: File Input/Output (I/O)

StreamReader 2

The end of the file is reachedwhen line is nothing

Page 17: File Input/Output (I/O)

Demonstration

StreamReader

Page 18: File Input/Output (I/O)

StreamWriter Allows a program to write characters to a file

WriteLine includesa carriage return

Name of the filethat will be written

Close releases any resourcesused by the system

Page 19: File Input/Output (I/O)

Demonstration

StreamWriter

Page 20: File Input/Output (I/O)

Handles situationwhen file not found

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.

Causesan error

Handles othersituations

Page 21: File Input/Output (I/O)

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.

Page 22: File Input/Output (I/O)

Demonstration

File I/O Exceptions

Page 23: File Input/Output (I/O)

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

Page 24: File Input/Output (I/O)

Open Dialog Box

Provides GUI for selecting a file to open

Page 25: File Input/Output (I/O)

Open Dialog Box GUI

The properties can be set using the GUI

There are manyproperties

Page 26: File Input/Output (I/O)

Open Dialog Box GUI Code But code must be used to display it and handle

what the user does with it.

Page 27: File Input/Output (I/O)

Demonstration

OpenFileDialog with GUI and Code

Page 28: File Input/Output (I/O)

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

Page 29: File Input/Output (I/O)

Demonstration

OpenFileDialog with Only Code

Page 30: File Input/Output (I/O)

Save Dialog Box

Provides GUI for inputting the name and location of a file to save

Page 31: File Input/Output (I/O)

Save Dialog Box GUI

The properties can be set using the GUI

There are manyproperties

Page 32: File Input/Output (I/O)

Save Dialog Box GUI Code But code must be used to display it and handle

what the user does with it.

Page 33: File Input/Output (I/O)

Demonstration

SaveFileDialog with GUI and Code

Page 34: File Input/Output (I/O)

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

Page 35: File Input/Output (I/O)

Demonstration

SaveFileDialog with Only Code