The Namespace
System.IO provides all the necessary classes, methods, and properties for manipulating directories and files. Table 1 elaborates the main classes under this namespace.
Class | Purpose/Use |
Binary Reader and Writer | Read and write primitive data types |
Directory, File, DirectoryInfo, and FileInfo | Create, delete, and move files and directories. Get specific information about the files by making use of the properties defined in these classes. |
FileStream | Access the files in a random fashion |
MemoryStream | Access data stored in memory |
StreamWriter and StreamReader | Read and write textual information |
StringReader and StringWriter | Read and write textual Information from a string buffer |
Table 1—Classes under System.IO
Creating Files by Using the FileInfo Class
With the FileInfo class, you can create new files, access information about the files, delete, and move files. This class also provides methods for opening, reading from, and writing to a file. Below code shows how to create a text file and access its information like its creation time, full name, and so forth.
FileInfo fi = new FileInfo(@"D:\test1.txt");
FileStream fstr = fi.Create();
Console.WriteLine("Creation Time: {0}",f.CreationTime);
Console.WriteLine("Full Name: {0}",f.FullName);
Console.WriteLine("FileAttributes: {0}",f.Attributes.ToString());
//Way to delete test1.txt file.
Console.WriteLine("Press any key to delete the file");
Console.Read();
fstr.Close();
fi.Delete();
The Open() Method
The FileInfo class defines a method named Open() with which you can create files by applying the values of the FileMode and FileAccess enumerations. The following code snippet describes its usage:
FileInfo f = new FileInfo("c:\myfile.txt");
FileStream s = f.Open(FileMode.OpenorWrite, FileAccess.Read);
You then can read from and write to a file by using the object 's'. In the overloaded Open() method, permission is given only for reading from a file. If you want to write to a file, you have to apply the ReadWrite value of FileAccess enumeration. Tables 4 and 5 describe the values of the FileMode and FileAccess enumerations.
Values | Purpose/Use |
Append | Opens the file and adds data. This should be used with the FileAccess Write Enumeration value. |
Create | Creates a new file. Overwrites any existing file. |
CreateNew | Creates a new file. If the file already exists, IOException is thrown. |
Open | Opens an existing file |
OpenOrCreate | Opens a new file. If there is no file, it creates a new file. |
Truncate | Truncates an existing file |
Table 4—FileMode Enumeration values
Values | Purpose/Use |
Read | Data can be read (retrieved) from the file |
ReadWrite | Data can be added to and retrieved from the file |
Write | Data can be added to the file |
Table 5—FileAccess Enumeration values
Writing to a Text File by Using the StreamWriter Class
You can easily write texts or other information to a file by using the CreateText() method of the FileInfo class. However, you have to obtain a valid StreamWriter. It's this StreamWriter reference that provides the required functionalities for writing to a file. To illustrate, following code snippet writes a series of texts to the Mytext.txt file.
Example 2:
FileInfo f = new FileInfo("Mytext.txt")
StreamWriter w = f.CreateText();
w.WriteLine("This is from");
w.WriteLine("Chapter 6");
w.WriteLine("Of C# Module");
w.Write(w.NewLine);
w.WriteLine("Thanks for your time");
w.Close();
Reading from a Text File
You can read from a Text file by using the StreamReader class. For this, you have to specify the file name using the static OpenText() method of the File class. Following code reads the contents that we have written in above code (Example 2):
Console.WriteLine("Reading the contents from the file");
StreamReader s = File.OpenText("Mytext.txt");
string read = null;
while ((read = s.ReadLine()) != null)
{
Console.WriteLine(read);
}
s.Close();
Thanks and enjoy it…….
No comments:
Post a Comment