![]() |
Details on File Processing |
In previous lessons, to handle exceptions, we were using the try, catch, and throw keywords. These allowed us to perform normal assignments in a try section and then handle an exception, if any, in a catch block. We also mentioned that, when you create a stream, the operating system must allocate resources and dedicate them to the file processing operations. Additional resources may be provided for the object that is in charge of writing to, or reading from, the stream. We also saw that, when the streaming was over, we should free the resources and give them back to the operating system. To do this, we called the Close() method of the variable that was using resources. More than any other assignment, file processing is in prime need of exception handling. During file processing, there are many things that can go wrong. For this reason, the creation and/or management of streams should be performed in a try block to get ready to handle exceptions that would occur. Besides actually handling exceptions, the C# language provides a special keyword used free resources. This keyword is finally. The finally keyword is used to create a section of an exception. Like catch, a finally block cannot exist by itself. It can be created following a try section. The formula used would be: try { } finally { } Based on this, the finally section has a body of its own, delimited by its curly brackets. Like catch, the finally section is created after the try section. Unlike catch, finally never has parentheses and never takes arguments. Unlike catch, the finally section is always executed. Because the finally clause always gets executed, you can include any type of code in it but it is usually appropriate to free the resources that were allocated previously. Here is an example: |
using System; using System.IO; class Exercise { static int Main() { string NameOfFile = "Members.clb"; FileStream fstPersons = new FileStream(NameOfFile, FileMode.Create); BinaryWriter wrtPersons = new BinaryWriter(fstPersons); try { wrtPersons.Write("James Bloch"); wrtPersons.Write("Catherina Wallace"); wrtPersons.Write("Bruce Lamont"); wrtPersons.Write("Douglas Truth"); } finally { wrtPersons.Close(); fstPersons.Close(); } return 0; } }
In the same way, you can use a finally section to free resources used when reading from a stream:
using System; using System.IO; class Exercise { static int Main() { / string NameOfFile = "Members.clb"; FileStream fstMembers = new FileStream(NameOfFile, FileMode.Create); BinaryWriter wrtMembers = new BinaryWriter(fstMembers); try { wrtMembers.Write("James Bloch"); wrtMembers.Write("Catherina Wallace"); wrtMembers.Write("Bruce Lamont"); wrtMembers.Write("Douglas Truth"); } finally { wrtMembers.Close(); fstMembers.Close(); } / string NameOfFile = "Members.clb"; string strLine = null; FileStream fstMembers = new FileStream(NameOfFile, FileMode.Open); BinaryReader rdrMembers = new BinaryReader(fstMembers); try { strLine = rdrMembers.Readstring(); Console.WriteLine(strLine); strLine = rdrMembers.Readstring(); Console.WriteLine(strLine); strLine = rdrMembers.Readstring(); Console.WriteLine(strLine); strLine = rdrMembers.Readstring(); Console.WriteLine(strLine); } finally { rdrMembers.Close(); fstMembers.Close(); } return 0; } }
Of course, since the whole block of code starts with a try section, it is used for exception handling. This means that you can add the necessary and appropriate catch section(s) but you don't have to.
In the previous lesson as our introduction to file processing, we behaved as if everything was alright. Unfortunately, file processing can be very strict in its assignments. Based on this, the .NET Framework provides various Exception-oriented classes to deal with almost any type of exception you can think of. One of the most important aspects of file processing is the name of the file that will be dealt with. In some cases you can provide this name to the application or document. In some other cases, you would let the user specify the name of the path. Regardless of how the name of the file would be provided to the operating system, when this name is acted upon, the compiler be asked to work on the file. If the file doesn't exist, the operation cannot be carried. Furthermore, the compiler would throw an error. There are many other exceptions that can thrown as a result of something going bad during file processing: FileNotFoundException: The exception thrown when a file has been found is of type FileNotFoundException. Here is an example of handling it: using System; using System.IO; class Exercise { static int Main() { / string NameOfFile = "Members.clb"; FileStream fstMembers = new FileStream(NameOfFile, FileMode.Create); BinaryWriter wrtMembers = new BinaryWriter(fstMembers); try { wrtMembers.Write("James Bloch"); wrtMembers.Write("Catherina Wallace"); wrtMembers.Write("Bruce Lamont"); wrtMembers.Write("Douglas Truth"); } finally { wrtMembers.Close(); fstMembers.Close(); } / string NameOfFile = "Associates.clb"; string strLine = null; try { FileStream fstMembers = new FileStream(NameOfFile, FileMode.Open); BinaryReader rdrMembers = new BinaryReader(fstMembers); try { strLine = rdrMembers.Readstring(); Console.WriteLine(strLine); strLine = rdrMembers.Readstring(); Console.WriteLine(strLine); strLine = rdrMembers.Readstring(); Console.WriteLine(strLine); strLine = rdrMembers.Readstring(); Console.WriteLine(strLine); } finally { rdrMembers.Close(); fstMembers.Close(); } } catch(FileNotFoundException ex) { Console.Write("Error: " + ex.Message); Console.WriteLine(" May be the file doesn't exist or you typed it wrong!"); } return 0; } } Here is an example of what this would produce: Error: Could not find file "C:\Programs\MSVCS .NET 2003\Project1\bin\Debug\Assoc iates.clb". May be the file doesn't exist or you typed it wrong! IOException: As mentioned already, during file processing, anything could go wrong. If you don't know what caused an error, you can throw the IOException exception.
Besides checking the existence of the file, the File class can be used to create a new file. To support this operation, the File class is equipped with the Create() method that is overloaded with two versions as follows: public static FileStream Create(string path); public static FileStream Create(string path, int buffersize); In both cases, the File.Create() method returns a Stream value, in this case a FileStream value. As the File.Create() method indicates, it takes the name or path of the file as argument. If you know or want to specify the size, in bytes, of the file, you can use the second version. To provide the same operation of creating a file, you can use the Open() method of the File class. It is overloaded in three versions as follows: public static FileStream Open( string path, FileMode mode ); public static FileStream Open( string path, FileMode mode, FileAccess access ); public static FileStream Open( string path, FileMode mode, FileAccess access, FileShare share );
A directory is a section of a medium used to delimit a group of files. Because it is a "physical" area, it can handle operations not available on files. In fact, there are many fundamental differences between both:
The similarities of both types are:
Before using a directory, you must first have it. You can use an existing directory if the operating system or someone else had already created one. You can also create a new directory. Directories are created and managed by various classes but the fundamental class is Directory. Additional operations are performed using the DirectoryInfo class. Before using or creating a directory, you can first check if it exists. This is because, if a directory already exists in the location where you want to create it, you would be prevented from creating one with the same name. In the same way, if you just decide to directly use a directory that doesn't exist, the operation you want to perform may fail because the directory would not be found. Before using or creating a directory, to check first whether it exists or not, you can call the Directory.Exists() Boolean method. Its syntax is: public static bool Exists(string path); This method receives the (complete) path of the directory. If the path is verified, the method returns true. If the directory exists, the method returns false. To create a directory, you can call the CreateDirectory() method of the Directory class.
|
|
||
Previous | Copyright © 2004-2010 FunctionX, Inc. | |
|