关于File.ReadAllText Method (String)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wolf96/article/details/75093533

File.ReadAllText Method (String)

用在读取电量了,但是发现他没有关闭流,查了一下

https://msdn.microsoft.com/en-us/library/ms143368(v=vs.110).aspx

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.


读取全部行的信息,之后就自己关闭了,mark一下


using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // This text is added only once to the file.
        if (!File.Exists(path))
        {
            // Create a file to write to.
            string createText = "Hello and Welcome" + Environment.NewLine;
            File.WriteAllText(path, createText);
        }

        // This text is always added, making the file longer over time
        // if it is not deleted.
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText);

        // Open the file to read from.
        string readText = File.ReadAllText(path);
        Console.WriteLine(readText);
    }
}



by wolf96 2017/7/13




猜你喜欢

转载自blog.csdn.net/wolf96/article/details/75093533