File Handling Using C#
The .NET Framework provides a extensive collection of base classes in the .NET Framework which make most of the work of file handling an efficient task, with the developer needed to write only the calls to these classes.
To use the file handling classes efficiently , you may want to review the classes and their methods under the package System.IO. We will cover some of the important classes, in our use-and-study methodology through the code samples itself.
Let us see in action how the file handling is done using .NET file handling classes:
Reading a Text File
To read a text file , we will use the class StreamReader which provides an implementation of the Abstract class, TextReader and hence supports the following methods :
ReadToEnd() -- Reads all characters to the end of the TextReader and returns them as one string
Let us write a class which reads a text file and outputs its contents to the system console.
Please type the following code using your favourite text editor:
Compile the program using the C# Compiler:
Run the Program:
Before you run the program , create a flat file with the name "samplefile.txt" and put any text in it and save
it in the same directory as the program.
Now you are ready to run the program, which will read this file and output the text to the system console:
Writing a Text File
We will use the class StreamWriter to write to a file. When we write to a file there are multiple possibilities, that the file may or may not exist, if the file is existing, we may have to specify whether the program should append to the existing file or overwrite the file.
The StreamWriter has the following constructors :
StreamWriter(System.String) Constructor
This constructor takes one paramter as String, which needs to be a valid filename.
StreamWriter(System.String, bool) Constructor
This constructor takes an additional parameter which is bool, this paramter is the flag for overwriting the file if it exists.
StreamWriter(System.IO.Stream) Constructor
StreamWriter(System.IO.Stream, System.Text.Encoding) Constructor
StreamWriter(System.IO.Stream, System.Text.Encoding, int) Constructor
StreamWriter(System.String, bool, System.Text.Encoding) Constructor
StreamWriter(System.String, bool, System.Text.Encoding, int) Constructor
StreamWriter Methods
StreamWriter.Write(System.String) Method
This method takes a string to write to the file.
StreamWriter.WriteLine(System.String) Method
This method is inherited from the TextWriter class,this method takes a string and writes to the file with a newline character.
StreamWriter.Write(char[], int, int) Method
StreamWriter.Write(char[]) Method
StreamWriter.Write(char) Method
StreamWriter.Close Method
StreamWriter.Dispose Method
StreamWriter.Finalize Method
StreamWriter.Flush Method
Let us now create a program which creates a new text file and writes to it:
Type in the following program using your favourite text editor:
Compile the program:
Run the program to do a dir on the directory to see if a new file is created:
As you will see a new file has been created and our text has been printed to that file.
0 comments:
Post a Comment