Question

This is the way I read file:

    public static string readFile(string path)
    {
        StringBuilder stringFromFile = new StringBuilder();
        StreamReader SR;
        string S;
        SR = File.OpenText(path);
        S = SR.ReadLine();
        while (S != null)
        {
            stringFromFile.Append(SR.ReadLine());
        }
        SR.Close();
        return stringFromFile.ToString();
    }

The problem is it so long (the .txt file is about 2.5 megs). Took over 5 minutes. Is there a better way?

Solution taken

    public static string readFile(string path)
    {

       return File.ReadAllText(path);

    }

Took less than 1 second... :)

Was it helpful?

Solution

Leaving aside the horrible variable names and the lack of a using statement (you won't close the file if there are any exceptions) that should be okay, and certainly shouldn't take 5 minutes to read 2.5 megs.

Where does the file live? Is it on a flaky network share?

By the way, the only difference between what you're doing and using File.ReadAllText is that you're losing line breaks. Is this deliberate? How long does ReadAllText take?

OTHER TIPS

S = SR.ReadLine();
while (S != null)
{
    stringFromFile.Append(SR.ReadLine());
}

Of note here, S is never set after that initial ReadLine(), so the S != null condition never triggers if you enter the while loop. Try:

S = SR.ReadLine();
while (S != null)
{
    stringFromFile.Append(S = SR.ReadLine());
}

or use one of the other comments.

If you need to remove newlines, use string.Replace(Environment.NewLine, "")

return System.IO.File.ReadAllText(path);

Marcus Griep has it right. IT's taking so long because YOU HAVE AN INFINITE LOOP. copied your code and made his changes and it read a 2.4 M text file in less than a second.

but I think you might miss the first line of the file. Try this.


S = SR.ReadLine();
while (S != null){
    stringFromFile.Append(S);
    S = SR.ReadLine();
}

Do you need the entire 2.5 Mb in memory at once?

If not, I would try to work with what you need.

Use System.IO.File.RealAllLines instead.

http://msdn.microsoft.com/en-us/library/system.io.file.readalllines.aspx

Alternatively, estimating the character count and passing that to StringBuilder's constructor as the capacity should speed it up.

Try this, should be much faster:

var str = System.IO.File.ReadAllText(path);
return str.Replace(Environment.NewLine, "");

By the way: Next time you're in a similar situation, try pre-allocating memory. This improves runtime drastically, regardless of the exact data structures you use. Most containers (StringBuilder as well) have a constructor that allow you to reserve memory. This way, less time-consuming reallocations are necessary during the read process.

For example, you could write the following if you want to read data from a file into a StringBuilder:

var info = new FileInfo(path);
var sb = new StringBuilder((int)info.Length);

(Cast necessary because System.IO.FileInfo.Length is long.)

ReadAllText was a very good solution for me. I used following code for 3.000.000 row text file and it took 4-5 seconds to read all rows.

string fileContent = System.IO.File.ReadAllText(txtFilePath.Text)
string[] arr = fileContent.Split('\n');

The loop and StringBuilder may be redundant; Try using ReadToEnd.

To read a text file fastest you can use something like this

public static string ReadFileAndFetchStringInSingleLine(string file)
    {
        StringBuilder sb;
        try
        {
            sb = new StringBuilder();
            using (FileStream fs = File.Open(file, FileMode.Open))
            {
                using (BufferedStream bs = new BufferedStream(fs))
                {
                    using (StreamReader sr = new StreamReader(bs))
                    {
                        string str;
                        while ((str = sr.ReadLine()) != null)
                        {
                            sb.Append(str);
                        }
                    }
                }
            }
            return sb.ToString();
        }
        catch (Exception ex)
        {
            return "";
        }
    }

Hope this will help you. and for more info, please visit to the following link- Fastest Way to Read Text Files

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top