Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# I/O - Difference between System.IO.File and StreamWriter/StreamReader

Tags:

c#

file-io

Assuming I am interested only in dealing with text files, what specific advantages or disadvantages does System.IO.File methods provide when compared to StreamWriter?

Are there any performance factors involved? What is the basic difference, and which of these should be used in what cases?

One more question, If i want to read the contents of a file into a string and run a LINQ query over it, which is best?

like image 652
Deepak Avatar asked Oct 13 '10 13:10

Deepak


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

There's a bit of interesting history behind the seemingly duplicate methods in the File class. It came about after a usability study on a pre-release version of .NET. They asked a group of experienced programmers to write code to manipulate files. They were never exposed to .NET before and just had the docs to work from. The success rate was 0%.

Yes, there's a difference. You'll find out when you try to read a file that's a gigabyte or more. That's a guaranteed crash on the 32-bit version. No such problem with a StreamReader that reads line-by-line, it will use very little memory. It depends on what the rest of your program does but try to limit the convenience method to files no larger than, say, a couple of megabytes.

like image 159
Hans Passant Avatar answered Oct 07 '22 04:10

Hans Passant


Generally I'd go with System.IO.File over StreamReader as the former is mostly a convenient wrapper for the latter. consider the code behind File.OpenText:

public static StreamReader OpenText(string path)
{
    if (path == null)
    {
        throw new ArgumentNullException("path");
    }
    return new StreamReader(path);
}

Or File.ReadAllLines:

private static string[] InternalReadAllLines(string path, Encoding encoding)
{
    List<string> list = new List<string>();
    using (StreamReader reader = new StreamReader(path, encoding))
    {
        string str;
        while ((str = reader.ReadLine()) != null)
        {
            list.Add(str);
        }
    }
    return list.ToArray();
}

You can use Reflector to check out some other methods, as you can see it's pretty straightforward

For reading the contents of the file, take a look at:

  • File.ReadAllLines
  • File.ReadAllText
like image 21
Ohad Schneider Avatar answered Oct 07 '22 04:10

Ohad Schneider