標籤:.exe iter binary 擷取對象 creat toe sys XML type
1.TextWriter(Reader)分別是對連續字元系列處理的編寫器(讀寫器),來自System.IO
2.StringWriter(Reader)繼承TextWriter(Reader),它主要是對字串進行處理的類,範例如下:
TextWriter stringwriter = new StringWriter();
stringwriter.Write("你好,通過StringWriter寫入,StringWriter繼承TextWriter");
TextReader stringreader = new StringReader(stringwriter.ToString());
Console.WriteLine("從{0}——{1}", stringreader.GetType().Name, stringreader.ReadToEnd());
Console.WriteLine();
3.StreamWriter(Reader)也繼承TextWriter(Reader),它主要是以流的形式處理資料,可以是記憶流,檔案流,系列連接埠流以及媒體視頻流等。例子:
using (TextWriter streamwriter = new StreamWriter("mytext.txt"))
{
streamwriter.Write("你好,通過StreamWriter寫入,StreamWriter繼承TextWriter");
}
TextReader streamreader = new StreamReader("mytext.txt");
Console.WriteLine("從{0}——{1}", streamreader.GetType().Name, streamreader.ReadToEnd());
Console.ReadLine();
4.BinaryWriter(Reader)不繼承TextWriter(Reader),以二進位形式講基元類型寫入(讀取),並支援特定的編碼,其有廣泛的支援形式,因為它以二進位方式讀取寫入。例子:
//BinaryWriter(Reader)
const string filename = "test.txt";
if (File.Exists(filename))
{
Console.WriteLine("{0}檔案已存在!", filename);
}
FileStream fs = new FileStream(filename, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
string str = Environment.CurrentDirectory + @"QQ.EXE.lnk";
bw.Write(str);
bw.Close();
fs.Close();
//現在開始讀取
fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long size = br.BaseStream.Length;
byte[] bytesize = new byte[size];
bytesize = br.ReadBytes((int)size);
Console.WriteLine(bytesize.ToString());
br.Close();
fs.Close();
Console.ReadLine();
下面再示一例BinaryWriter寫入XML資訊例子:
const string filename = "XmlWriter1.xml";
FileStream fs = new FileStream(filename, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
XmlElement ee = xdoc.DocumentElement;
if (ee != null)
{
bw.Write(ee.OuterXml);
}
else
{
Console.WriteLine("擷取對象失敗!");
}
bw.Close();
fs.Close();
C#學習筆記---區分StringWriter(Reader)和StreamWriter(Reader),TextWriter(Reader),BinaryWriter(Reader)