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?
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? 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.
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.
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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With