1。文件或者目录操作可以用到FileInfo,File,DirectoryInfo,Directory类:
FileInfo和File的主要区别是File的多数方法都是静态方法,DirectoryInfo与Directory区别与此相似。
2。流:In the world of I/O
manipulation, a stream represents a chunk of data. Streams provide a common way to interact with
a sequence of bytes, regardless of what kind of device (file, network connection, printer, etc.) is storing
or displaying the bytes in question.
在实际操作中,我们很少直接使用FileStream,因为它只接受直接数组类型的数据。取而代之的我们使用StreamReader, and StreamWriter这对对FileStream更好的包装。
3。Reader 和 Writer对:
Using the StringWriter and StringReader types, you can treat textual information as a stream of
in-memory characters. This can prove helpful when you wish to append character-based information
to an underlying buffer.
4代码例子:
Code
using System;
using System.IO;
namespace StreamWriterReaderApp
{
public class MyStreamWriterReader
{
static void Main(string[] args)
{
Console.WriteLine("***** Fun with StreamWriter / StreamReader *****\n");
// Get a StreamWriter and write string data.
// StreamWriter writer = File.CreateText("Thoughts.txt");
StreamWriter writer = new StreamWriter("reminders.txt");
writer.WriteLine("Don't forget Mother's Day this year");
writer.WriteLine("Don't forget Father's Day this year");
writer.WriteLine("Don't forget these numbers:");
for(int i = 0; i < 10; i++)
writer.Write(i + " ");
// Insert a new line.
writer.Write(writer.NewLine);
// Closing automatically flushes!
writer.Close();
Console.WriteLine("Created file and wrote some thoughts");
// Now read data from file.
Console.WriteLine("Here are your thoughts:\n");
// StreamReader sr = File.OpenText("reminders.txt");
StreamReader sr = new StreamReader("reminders.txt");
string input = null;
while ((input = sr.ReadLine()) != null)
{
Console.WriteLine (input);
}
}
}
}