稍微总结下,System.IO提供了四种类型来实现,对单个文件和计算机目录结构的操作。Directory和File通过静态成员实现建立、删除、复制和移动操作(上图没有提及)。而FileInfo和DirectryInfo类型则通过实例级方法来实现类似的功能,并且更加推荐使用,原因是它们的成员方法返回强类型的对象。
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差异与用途
总觉得,他们很像,感觉做的事情很像,所以查了一下。
1.用Reflector反射看了下StringWriter,发现其还是用StringBuilder干事情。下面是他的Write方法。他里边使用了StringBuilder类的全局变量。同时,他还提供了GetStringBuilder方法,通过这个方法可以获取这个全局变量
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.
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.
3.StringBuilder,String差异
String 对象串联操作总是用现有字符串和新数据创建新的对象。
StringBuilder 对象维护一个缓冲区,以便容纳新数据的串联。 如果房间可用,新数据追加到缓冲区;否则,新的分配,较大的缓冲区,从原始缓冲区的数据复制到新的缓冲区,并且,新数据并追加到新的缓冲区。
串联操作的性能 String 或 StringBuilder 对象的取决于内存分配的频率。String 串联运算始终分配内存,而 StringBuilder串联运算分配内存,仅当 StringBuilder 对象缓冲区因过小而无法适应新数据。
如果是连接固定的数量的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)