Chapter 18. File I/O

File I/O functionality in C# depends largely on the underlying BCL. There are no keywords in C# for dealing with file I/O specifically. [1] This chapter will familiarize you with some of the common file I/O operations so that you can start using them immediately.

[1] This is the same case for Java too. File I/O depends wholly on the underlying Java core classes rather than the language itself.

The classes will not be described fully, and there is usually more than one way to accomplish something. You will be expected to refer to the API documentation for more information.

In order to execute the codes in this chapter, you need the necessary permissions to manipulate your file system. [2]

[2] Permissions and security will not be discussed here, but if you are experimenting on your local Windows machine and you have administrator privileges, you should be able to run the codes without any security exceptions.

I shall discuss how to manipulate files and directories [3] using the Directory, File, DirectoryInfo, and FileInfo classes. This is followed by sections on how to read/write from text files and binary files.

[3] Directories are also known as 'folders' in Windows nomenclature. However, Microsoft has selected to use 'directory' instead of folder for naming purposes in the .NET class libraries.

Figure 18.1 lists the classes that are important in file I/O. Table 18.1 lists the classes involved in I/O operations and Table 18.2 gives some exception classes.

Figure 18.1. Important classes used for I/O operations. Shaded classes are in the System.IO namespace – unshaded classes are in the System namespace.


Table 18.1. Some classes involved in I/O operations and their description
ClassDescription
FileInfoRepresents a file
FileRepresents a file – contains only static methods, and cannot be instantiated
DirectoryInfoRepresents a directory – exposes instance methods for creating and moving through directories and subdirectories
DirectoryRepresents a directory – contains only static methods for creating and moving through directories and subdirectories; this class cannot be instantiated
PathContains static members for manipulating path names
StreamReaderImplements a TextReader that reads characters from a byte stream
StreamWriterImplements a TextWriter that writes characters to a byte stream
StringReaderImplements a TextReader that reads from a string
StringWriterWrites to a string
TextReaderA reader that reads a sequential series of characters
TextWriterA writer that writes a sequential series of characters
FileStreamExposes a Stream object around a file for synchronous and asynchronous read/write operations

Table 18.2. Important exception classes for I/O operations – all these exception classes are in the System.IO namespace
Exception classDescription
FileNotFoundExceptionThrown when a file cannot be found
DirectoryNotFoundExceptionThrown when part of a file or directory cannot be found
EndOfStreamExceptionThrown when an attempt is made to read past end of a stream
IOExceptionThrown when an general I/O error occurs – for example, when an attempt is made to copy a file to a target path which already contains a file of the same name
PathTooLongExceptionThrown when a path or file name is longer than the maximum length defined by the system
FileLoadExceptionThrown when file is found but cannot be loaded

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.232.191