3PRB C# Friday March 24th 2013

Today I learnt about Reading and writing files, using StreamReader and StreamWriter

StreamReader fileSR = new StreamReader("test.txt");
while(fileSR.Peek() != -1)
{
Console.WriteLine(fileSR.ReadLine());
}
fileSR.Close();

Learnt that not closing the file can result in a corrupt file.

Syntax is similar for writing a file:
Array is assumed to be declared already.

StreamWriter fileSW = new StreamWriter("test.txt");
for(int index = 0; index < arrayData.Length; index++)
{
  fileSW.WriteLine(arrayData[index]);
}

Was this helpful?

Yes No


Comments