103k views
2 votes
Suppose there is a text file named "datafile.txt with some data in the root directory of your computer". Using your choice of language, C#, Java, or pseudocode, write code that (i) opens the file in read mode; (ii) displays the file content on the monitor screen; (iii) uses exception handling mechanism; and (iv) safely closes the file. You don't need to write the main() method.

User Mgutt
by
4.5k points

1 Answer

0 votes

Answer:

See explaination for program code

Step-by-step explanation:

code below

using System;

using System.IO;

namespace ReadAndDisplayFileCOnsole

{

class Program

{

static void Main(string[] args)

{

using (StreamReader reader = new StreamReader("D:\\datafile.txt"))

{

while (true)

{

string line = reader.ReadLine();

if (line == null)

{

break;

}

Console.WriteLine(line);

}

reader.Close();

Console.Read();

}

}

}

}

User Amirhe
by
4.0k points