开发者

How to read Lengthy text File Efficently?

What if you have a big text file that has many entries in it you have to get those and save them to List is there any faster way to do that rather than traditional way as in my code

    /// <summary>
    /// Reads Data from the given path
    /// </summary>
    /// <param name="path">Path of the File to read</param>
    public void Read(string path)
    {
        try
        {
            FileStream file = new FileStream(path, FileMode.Open);
            StreamReader reader = new StreamReader(file);
            string line;

            while ((line = reader.ReadLine()) != null)
            {                
                myList.Add(line);
                System.Windows.Forms.Application.DoEvents();
            }
            reader.Close();
            file.Close();
        }
        catch (Exception c)开发者_StackOverflow社区
        {
            MessageBox.Show(c.Message);
        }

    }


Faster - probably not, but much shorter? yes:

myList.AddRange(File.ReadAllLines(path));

Also if this is a very big file you shouldn't be doing the loading in the UI thread, just use a background worker - that would save you from calling Application.DoEvents() on every line and probably will make this somewhat faster than your original approach - although compared to the cost of File I/O the savings will be minimal, but you will have cleaner code.


Normally I'd make this a comment, but it's actually an answer in this case.

No there isn't. You can't read a file any faster than ... reading it!

Reading a file is linear time, hands down.

Now, if you knew some information about the file, say... you wanted to skip some header information, THAT you could do by skipping ahead. But if the place in the file is dependant on the previous data (not fixed width) or you need every line in it, no it's not going to get faster than that.


You are going to have to get rid of DoEvents(). Just try it: close the form while it is busy reading the file. Note that your program won't stop. Odds are good that it then crashes and dies when the reading is completed.

This ought to speed up the file reading a good bit as well. To prevent the UI from freezing you should use a BackgroundWorker. Be sure to read the MSDN Library articles for them to get this right.

Making it faster still is going to require hardware. Start by questioning why you have to read such large text files, the info in that file probably belongs in a database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜