Reading large text files with streams in C#

后端 未结 11 1839
野的像风
野的像风 2020-11-22 08:28

I\'ve got the lovely task of working out how to handle large files being loaded into our application\'s script editor (it\'s like VBA for our internal product for quick macr

11条回答
  •  佛祖请我去吃肉
    2020-11-22 09:23

    All excellent answers! however, for someone looking for an answer, these appear to be somewhat incomplete.

    As a standard String can only of Size X, 2Gb to 4Gb depending on your configuration, these answers do not really fulfil the OP's question. One method is to work with a List of Strings:

    List Words = new List();
    
    using (StreamReader sr = new StreamReader(@"C:\Temp\file.txt"))
    {
    
    string line = string.Empty;
    
    while ((line = sr.ReadLine()) != null)
    {
        Words.Add(line);
    }
    }
    

    Some may want to Tokenise and split the line when processing. The String List now can contain very large volumes of Text.

提交回复
热议问题