.net I/O操做 導圖

IO

稍微總結下,System.IO提供了四種類型來實現,對單個文件和計算機目錄結構的操做。Directory和File經過靜態成員實現創建、刪除、複製和移動操做(上圖沒有說起)。而FileInfo和DirectryInfo類型則經過實例級方法來實現相似的功能,而且更加推薦使用,緣由是它們的成員方法返回強類型的對象。html

FileStream和StreamReader/StreamWriter的主要區別在於,FileStream操做的是字節/字節數組;而StreamReader/StreamWriter能夠操做字符串。數組

public  void CreateByteFile()
{
    var fileInfo = new FileInfo(string.Format(@"{0}\Test.dat", Environment.CurrentDirectory));
    using (var fileStream = fileInfo.Open(FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.None))
    {
        const string msg = "Hello";
        var msgAsByteArray = Encoding.Default.GetBytes(msg);
        fileStream.Write(msgAsByteArray,0,msgAsByteArray.Length);
        fileStream.Position = 0;
        Console.WriteLine("Your Message as an array of bytes:\n");
        var bytesFormFile = new byte[msgAsByteArray.Length];
        for (var i = 0; i < msgAsByteArray.Length; i++)
        {
            bytesFormFile[i] =(byte) fileStream.ReadByte();
            Console.WriteLine(bytesFormFile[i]);
        }
        Console.WriteLine("\n Decoded Message: ");
        Console.WriteLine(Encoding.Default.GetString(bytesFormFile));
    }
}
public void WriteCharFile()
{
    var fileInfo = new FileInfo(string.Format( "{0}reminders.txt", AppDomain.CurrentDomain.BaseDirectory));
    using (var streamWriter = fileInfo.CreateText())
    {
        streamWriter.WriteLine("Don't forget Mother's Day this year...");
        streamWriter.WriteLine("Don't forget Father's Day this year...");
        streamWriter.WriteLine("Don't forget These numbers:");
        for (var i = 0; i < 10; i++)
        {
                streamWriter.Write(i + "    ");
        }
        streamWriter.Write(streamWriter.NewLine);
    }
    Console.WriteLine("Created file and wrote some thoughts...");
    Console.ReadKey();
}

StringBuilder,StringWriter,String差別與用途

總以爲,他們很像,感受作的事情很像,因此查了一下。app

1.用Reflector反射看了下StringWriter,發現其仍是用StringBuilder幹事情。下面是他的Write方法。他裏邊使用了StringBuilder類的全局變量。同時,他還提供了GetStringBuilder方法,經過這個方法能夠獲取這個全局變量dom

private StringBuilder _sb;
public override void Write(string value)
{
    if (!this._isOpen)
    {
        __Error.WriterClosed();
    }
    if (value != null)
    {
        this._sb.Append(value);
    }
}
StringWriter和StringBuilder的最大差別在於StringWriter繼承於TextWriter。能夠將StringWriter簡單理解爲內部存儲使用StringBuiler的一個TextWriter的實現類。這樣你就能夠經過StringWriter來使用TestWriter的API了。(MSDN的神解釋:A StringWriter is simply an implementation of TextWriter that uses a StringBuilder internally for storage. So you use StringWriter with APIs that operate on TextWriters.)

2. StringWriter,StringBuilder的另外一種描述

StringWriter derives from TextWriter, which allows various classes to write text without caring where it's going. In the case of StringWriter, the output is just into memory. You would use this if you're calling an API which needs a TextWriter but you only want to build up results in memory. ide

StringBuilder is essentially a buffer which allows you to perform multiple operations (typically appends) to a "logical string" without creating a new string object each time. You would use this to construct a string in multiple operations. oop

3.StringBuilder,String差別 性能

String 對象串聯操做老是用現有字符串和新數據建立新的對象。 ui

StringBuilder 對象維護一個緩衝區,以便容納新數據的串聯。 若是房間可用,新數據追加到緩衝區;不然,新的分配,較大的緩衝區,從原始緩衝區的數據複製到新的緩衝區,而且,新數據並追加到新的緩衝區。 this

串聯操做的性能 String 或 StringBuilder 對象的取決於內存分配的頻率。String 串聯運算始終分配內存,而 StringBuilder串聯運算分配內存,僅當 StringBuilder 對象緩衝區因太小而沒法適應新數據。spa

若是是鏈接固定的數量的string字符串,建議使用string鏈接(Use the String class if you are concatenating a fixed number of String objects)。在這種狀況下,編譯器可能甚至合併單個的串聯運算到單個操做(In that case, the compiler may even combine individual concatenation operations into a single operation.)。使用一 StringBuilder 對象是否鏈接任意字符串;例如,在中,若是使用循環鏈接隨機數用戶輸入字符串。(Use a StringBuilder object if you are concatenating an arbitrary number of strings; for example, if you're using a loop to concatenate a random number of strings of user input)

相關文章
相關標籤/搜索