Here we have implemented file handling where we have created a file in local drive and opened the file edited it and again saved it
public class FileWrite
{
#Method to write data into file
public void WriteData()
{
FileStream fs = new FileStream("D:\\test.txt",FileMode.Append,FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
Console.WriteLine("Enter the text to write ");
String str = Console.ReadLine();
sw.WriteLine(str);
sw.Flush();
sw.Close();
fs.Close();
}
#Method to read data from file
public void ReadData()
{
FileStream fs = new FileStream("D:\\test.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
Console.WriteLine("Show Content");
sr.BaseStream.Seek(0, SeekOrigin.Begin);
String str = sr.ReadLine();
while(str != null)
{
Console.WriteLine(str);
str = sr.ReadLine();
}
Console.ReadLine();
sr.Close();
fs.Close();
}
}
FileWrite fileWrite = new FileWrite();
fileWrite.WriteData();
fileWrite.ReadData();
So finally we are able to read and write data in to file system and save the file into our local system.