Home:ALL Converter>C# open text file

C# open text file

Ask Time:2017-10-21T16:55:42         Author:Richard Rublev

Json Formatter

On doc.microsoft site,we have streamreader solution like this

using System;
using System.IO;

class Test
{
    public static void Main()
    {
        try
        {   // Open the text file using a stream reader.
            using (StreamReader sr = new StreamReader("TestFile.txt"))
            {
            // Read the stream to a string, and write the string to the console.
                String line = sr.ReadToEnd();
                Console.WriteLine(line);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }
}

But I have also come across different examples like this

FileStream fin = null;
try {
  fin = new FileStream("test", FileMode.Open);
}

catch(IOException exc) {
  Console.WriteLine(exc.Message);
}

Is there any advantage in defining nullable FileStream?

Author:Richard Rublev,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/46861649/c-sharp-open-text-file
yy