将文本文件读入字符串变量的最快方法是什么?

我理解它可以通过几种方式完成,比如读取单个字节,然后将它们转换为字符串。我在寻找一种编码最少的方法。


当前回答

用最少的c#代码最快的方法可能就是这个:

string readText = System.IO.File.ReadAllText(path);

其他回答

看一下File.ReadAllText()方法

一些重要备注:

This method opens a file, reads each line of the file, and then adds each line as an element of a string. It then closes the file. A line is defined as a sequence of characters followed by a carriage return ('\r'), a line feed ('\n'), or a carriage return immediately followed by a line feed. The resulting string does not contain the terminating carriage return and/or line feed. This method attempts to automatically detect the encoding of a file based on the presence of byte order marks. Encoding formats UTF-8 and UTF-32 (both big-endian and little-endian) can be detected. Use the ReadAllText(String, Encoding) method overload when reading files that might contain imported text, because unrecognized characters may not be read correctly. The file handle is guaranteed to be closed by this method, even if exceptions are raised

File的基准比较。c#文件处理中的ReadAllLines vs StreamReader ReadLine

结果。StreamReader对于10000 +的大文件要快得多 行,但对于较小的文件,差异可以忽略不计。像往常一样, 计划不同大小的文件,并使用File。仅当 性能并不重要。

StreamReader方法

作为文件。ReadAllText方法已经被其他人建议,您也可以尝试更快的方法(我没有定量测试性能影响,但它似乎比File更快。ReadAllText(参见下面的比较))。不过,只有在较大的文件中才能看到性能上的差异。

string readContents;
using (StreamReader streamReader = new StreamReader(path, Encoding.UTF8))
{
     readContents = streamReader.ReadToEnd();
}

File.Readxxx()与StreamReader.Readxxx()的比较

通过ILSpy查看指示性代码,我发现了以下关于文件的内容。ReadAllLines File.ReadAllText。

文件。ReadAllText -使用StreamReader。ReadToEnd内部 文件。ReadAllLines -也使用StreamReader。内部ReadLine,额外的开销是创建List<string>作为读取行返回,并循环直到文件结束。

所以这两个方法都是建立在StreamReader之上的额外便利层。这从该方法的指示体中可以明显看出。

由ILSpy反编译的File.ReadAllText()实现

public static string ReadAllText(string path)
{
    if (path == null)
    {
        throw new ArgumentNullException("path");
    }
    if (path.Length == 0)
    {
        throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
    }
    return File.InternalReadAllText(path, Encoding.UTF8);
}   

private static string InternalReadAllText(string path, Encoding encoding)
{
    string result;
    using (StreamReader streamReader = new StreamReader(path, encoding))
    {
        result = streamReader.ReadToEnd();
    }
    return result;
}

File怎么样?ReadAllText:

string contents = File.ReadAllText(@"C:\temp\test.txt");
System.IO.StreamReader myFile =
   new System.IO.StreamReader("c:\\test.txt");
string myString = myFile.ReadToEnd();

string text = File.ReadAllText("Path");一个字符串变量中包含了所有文本。如果你需要每一行单独,你可以使用这个:

string[] lines = File.ReadAllLines("Path");