关于File.ReadAllText Method (String)

来源:互联网 发布:访问网站出现 php探针 编辑:程序博客网 时间:2024/06/07 14:56

File.ReadAllText Method (String)

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

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

Remarks

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




原创粉丝点击