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]);
}
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?
Comments
Post a Comment