09 iec t1_s1_oo_ps_session_13
TRANSCRIPT
Slide 1 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
In this session, you will learn to:Implement read and write in text files
Implement read and write in binary files
Implement the Windows File System
Describe exceptions
Objectives
Slide 2 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
• The Stream class is used to read from and to write data in the text files.
• It is an abstract class, which supports reading and writing bytes into it.
• If data of a file is only text, then you can use the StreamReader class and the StreamWriter class to accomplish the reading and writing tasks, respectively.
Implementing Reading and Writing in the Text Files
Slide 3 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
• The StreamReader class is inherited from the abstract class TextReader.
• The TextReader class represents a reader which can read a series of characters.
StreamReader Class
Slide 4 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
• The following table describes some of the commonly used methods of the StreamReader class.
StreamReader Class (Contd.)
Allows the read/write position to be moved to any position within the file Seek
Reads a line of characters from the current stream and returns data as a string
ReadLine
Reads the next character or the next set of characters from the streamRead
Returns the next available character but does not consume it Peek
Closes the object of StreamReader class and the underlying stream, and releases any system resources associated with the reader
Close
DescriptionMethods
Slide 5 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
• The StreamWriter class is inherited from the abstract class TextWriter.
• The TextWriter class represents a writer, which can write a series of characters.
• The following table describes some of the commonly used methods of the StreamWriter class.
StreamWriter Class
Writes data specified by the overloaded parameters, followed by end of line
WriteLine
Writes to the streamWrite
Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream
Flush
Closes the current StreamWriter object and the underlying streamClose
DescriptionMethods
Slide 6 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
Implementing Reading and Writing in Binary Files
• In C#, you can directly display the contents of the file on the screen.
• Binary read and write means that a number is written as a float representation consuming four bytes of space.
• The BinaryReader and BinaryWriter classes are used for reading and writing the binary data in to files.
Slide 7 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
BinaryReader Class
• The BinaryReader class is used to read binary data from a file.
• The following table describes some of the commonly used methods of the BinaryReader class.
Reads characters from the underlying stream and advances the current position of the stream
Read
Closes the current reader and the underlying stream Close
DescriptionMethod
Slide 8 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
• The BinaryWriter class is used to write binary data to a stream.
• The following table describes some of the commonly used methods of the BinaryWriter class.
Clears all buffers for the current writer and causes any buffered data to be written to the underlying device
Flush
Writes a value to the current stream Write
Sets the position within the current stream Seek
Closes the current BinaryWriter and the underlying stream Close
DescriptionMethod
BinaryWriter Class
Slide 9 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
Implementing the Windows File System
• The ability to browse and locate files and directories for a specific directory is essential for many programming tasks.
• Using classes such as the DirectoryInfo and FileInfo classes in combination is an efficient way to gather the required information about files and directories in a specific location.
Slide 10 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
DirectoryInfo Class
• The DirectoryInfo class is derived from the FileSystemInfo class.
• The following table describes some of the commonly used properties of the DirectoryInfo class.
Gets a string containing the name of a given file.Name
Gets the last accessed time of the directory. This property is inherited from FileSystemInfo.
LastAccessTime
Gets a string containing the full path of the directory. This property is inherited from FileSystemInfo.
FullName
Gets a string containing the file extension. This property is inherited from FileSystemInfo.
Extension
Gets a Boolean value indicating whether the directory exist or not.Exists
Gets or sets CreationTime of the current file. This property is inherited from FileSystemInfo.
CreationTime
Gets or sets attributes associated with the current file. This property is inherited from FileSystemInfo.
Attributes
DescriptionProperty
Slide 11 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
DirectoryInfo Class (Contd.)
• The following table describes some of the commonly used methods of the DirectoryInfo class.
Returns the files in the current directory.GetFiles
Returns the directories in the current directory after matching all the criteria. It also allows you to search subdirectories within directories.
GetDirectories
Deletes a directory.Delete
Creates a subdirectory.CreateSubdirectory
Creates a directory.Create
DescriptionMethod
Slide 12 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
FileInfo Class
• The FileInfo class is derived from FileSystemInfo class.
• The following table describes some commonly used properties FileInfo class.
Gets a string containing the name of a given file.Name
Gets the size of the file. Length
Gets the time of the last written activity to the file. This property is inherited from FileSystemInfo.
LastWriteTime
Gets the last accessed time of the file. This property is inherited from FileSystemInfo.LastAccessTime
Gets a string containing the full path of the file. This property is inherited from FileSystemInfo.
FullName
Gets a string containing the file extension. This property is inherited from FileSystemInfo.Extension
Gets a boolean value indicating whether the file exists or not.Exists
Gets an instance of the directory which the file belongs to.Directory
Gets or sets CreationTime of the current file. This property is inherited from FileSystemInfo.
CreationTime
Gets or sets attributes associated with the current file. This property is inherited from FileSystemInfo.
Attributes
DescriptionProperty
Slide 13 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
FileInfo Class (Contd.)
• The following table describes some of the commonly used methods of the FileInfo class.
Opens a file in read-only modeOpenRead
Opens file Open
Deletes a fileDelete
Appends a text to the file represented by the FileInfo objectAppendText
Creates a fileCreate
DescriptionMethod
Slide 14 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
An exception is an erroneous situation that occurs during program execution.
When an exception occurs in an application, the system throws an error.
The error is handled through the process of exception handling.
Describing Exceptions
Slide 15 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
There are three types of errors that can occur in the application: – Syntax errors: Occurs when statements are not constructed
properly, keywords are misspelled, or punctuation is omitted. – Run-time errors: Occurs when an application attempts to
perform an operation, which is not allowed at runtime.– Logical errors: Occurs when an application compiles and runs
properly but does not produce the expected results.
Let us understand the various types of errors in detail.
Types of Errors
Slide 16 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
class Errors
{
Console.WriteLine(“Enjoy Errors”)
}
Semicolon is missing from Console.WriteLine statement
Syntax Errors
Slide 17 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
class Errors
{
int Num1=0;int Num2=20;int Num3;
Num3=Num2/Num1;
Console.WriteLine(“The Result is {0}”, Num3);
}
Division by zero has taken place
Run-Time Errors
Slide 18 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
class Errors
{
int Num1=10;int Num2=2;int Num3;
Num3=Num2/Num1;
Console.WriteLine(“The Result is {0}”, Num3);
}
Expected result = 5
Present result = 0.2
Logical Errors
Slide 19 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
• There are many exception classes which are directly or indirectly derived from the System.Exception class. Some of these classes are:– System.ApplicationException class– System.SystemException class
Exception Classes
Slide 20 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
The hierarchy of the exception classes is displayed in the following figure.
Exception Classes (Contd.)
System.Exception
System.ApplicationException
System.SystemException
Slide 21 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
• The System.SystemException acts as a base class for all the predefined system exceptions. The following table describes some of the classes derived from the System.SystemException class.
Exception Classes (Contd.)
Handles memory allocation to the application errorsSystem.OutOfMemoryException
Handles errors generated during typecastingSystem.InvalidCastException
Handles errors generated during the process of dividing the dividend with zero
System.DivideByZeroException
Handles errors generated during the process of dereferencing a null object
System.NullReferenceException
Handles errors generated when a method refers to an array element, which is out of its bound
System.IndexOutOfRangeException
Handles I/O errorsSystem.IO.IOException
DescriptionException Classes
Slide 22 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
In this session, you learned that:– The Stream class is used to read and write data to the text
files. It is an abstract class, which supports reading and writing bytes into it.
– The StreamReader class is inherited from the abstract class TextReader. The TextReader class represents a reader which can read a series of characters.
– The StreamWriter class is inherited from the abstract class TextWriter. The TextWriter class represents a writer, which can write a series of characters.
– The BinaryReader class allows reading of binary data from a file.
– The BinaryWriter class allows writing of binary data to a stream.
Summary
Slide 23 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
– The DirectoryInfo class is derived from FileSystemInfo class and it works on a specific directory and shows the fullpath of the current directory.
– The FileInfo class is derived from FileSystemInfo class and it works on a specific directory to display the list of all the files.
– An exception is an erroneous situation that occurs during program execution.
– A syntax error occurs when a compiler cannot compile the code. This may happen when the statements are not constructed properly, keywords are misspelled, or punctuation is omitted.
– A run-time error occurs when an application attempts to perform an operation that is not allowed.
Summary (Contd.)
Slide 24 of 24Session 13Ver. 1.0
Object-Oriented Programming Using C#
A logical error occurs when an application compiles and runs properly but does not produce the expected results.
Exceptional conditions arise when an operation cannot be completed normally during the processing of C# statements and expressions.
Summary (Contd.)