using System;
using System.IO;
namespace Console_FileHandling_P_
{
class Program
{
public void writingText()
{
//For file Writing
FileStream fs = new FileStream(@"D:\C# Console Prog 2012\Demo.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
Console.WriteLine("Enter your Name :");
string nm = Console.ReadLine();
Console.WriteLine("Enter your age :");
int age = int.Parse(Console.ReadLine());
sw.WriteLine("Hello! Your Name is :" + nm);
sw.WriteLine("And age is : " + age);
sw.Flush();
sw.Close();
fs.Close();
Console.WriteLine("File has been created");
}
public void readingText()
{
// For file reading
FileStream fs = new FileStream(@"D:\C# Console Prog 2012\Demo.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string txt = sr.ReadLine();
while (txt != null)
{
Console.WriteLine(txt);
txt = sr.ReadLine();
}
sr.Close();
fs.Close();
}
static void Main(string[] args)
{
Program prog = new Program();
prog.writingText();
prog.readingText();
Console.ReadKey();
}
}
}
No comments:
Post a Comment