流(2)--流

BinaryReader,BinaryWriter

流(2)--流_第1张图片

Writer的方法主要有--Seek主要用来选取插入的位置,Write方法重载了写入bool,decimal,double,float,int,long,sbyte,short,string,unit,ulong,ushort,byte,byte[],char,char[]的方法。

 

流(2)--流_第2张图片

可以看见其中的几个方法--PeekChar,Read,Readxxx,对于byte和char有Readxxxs。Readxxx和Readxxxs行为基本一致,读一个类型,往后移,而且当读到尽头,再调用这个方法将抛出异常。Read有3个重载方法,第一个是Read,返回int,int是读取的值,当读到流的末尾时返回的是-1.第二个是Read到一个char[]中指定读到char[]的什么位置,要读几个字符,返回的是读取字符的个数,当读到流的末尾返回0,还有一个重载是byte[]类似于char[]。PeekChar是获取下一个char,返回读取的东东,但是流不往后走,也就是下次读还是这个字符。

StringWriter,StringReader

StringWriter类提供一个类似于Stream的方法连接字符串。字符串连接是一个开销很大的操作,因为字符串本身是不变的(immutable)类型。用 StringWriter来连接字符串会提供更好的性能。StringWriter类只是另一个对象 -- StringBuilder 的封装,StringBuilder才是真正用来执行字符串连接的类。你可以将StringBuilder作为一个参数构造器传入 StringWriter (否则会在其内部生成一个实例),并用GetStringBuilder方法返回StringBuilder。这是一个基类型,只接受和操作string,不参与修饰别的流。

http://topic.csdn.net/u/20090927/11/499c6042-df94-4345-93bc-afeaf7c49ee5.html

<textarea cols="50" rows="15" name="code" class="c-sharp"> class Program { static void Main(string[] args) { StringWriter sw = new StringWriter(); sw.WriteLine(&quot;xuxu&quot;); Console.WriteLine(sw); StringReader sr = new StringReader(&quot;xuxu&quot;); Console.WriteLine( sr.ReadLine() ); } }</textarea>

FileStream --以byte[]数组形式操作,所以是二进制的。支持Seek,支持异步

<textarea cols="50" rows="15" name="code" class="c-sharp"> class Program { static void Main(string[] args) { FileInfo info = new FileInfo(&quot;D:/a.txt&quot;); FileInfo info1 = new FileInfo(&quot;D:/b.bat&quot;); FileStream fr = info.OpenRead(); FileStream fw = info1.OpenWrite(); byte[] read = new byte[5]; int count = 0; while ( (count=fr.Read( read,0,5))&gt;0 ) fw.Write(read, 0, count); } }</textarea>

BufferedStream-- 带缓冲的流,是修饰类流。注意使用flush。

<textarea cols="50" rows="15" name="code" class="c-sharp">class Program { static void Main(string[] args) { FileInfo info = new FileInfo(&quot;D:/a.txt&quot;); FileInfo info1 = new FileInfo(&quot;D:/b.bat&quot;); FileStream fr = info.OpenRead(); FileStream fw = info1.OpenWrite(); BufferedStream bfr = new BufferedStream(fr); BufferedStream bfw = new BufferedStream(fw); byte[] read = new byte[1024]; int count = 0; while ( (count=fr.Read( read,0,1024))&gt;0 ) fw.Write(read, 0, count); bfr.Flush(); bfw.Flush(); bfr.Close(); bfw.Close(); } }</textarea>

StreamWriter,StreamReader --操作文本,提供了ReadLine,WriteLine方法就是证明。StreamReader构造器接受一个Stream对象或者一个文件路径 (你可以使用Universal Naming Convention [UNC] 路径,但不能用URL)。你还可以指定这些参数:比如编码类型(encoding type)(如果没有特别指定,系统会默认使用UTF8编码);作为缓存的内置缓冲器大小(很可能通过BufferedStream对象来实现);还有一 个布尔值,它用来指示是否应通过该Stream的第一个字节来判断编码类型。可以看到,StreamReader是个很有用的封装(wrapper)类

<textarea cols="50" rows="15" name="code" class="c-sharp">class Program { static void Main(string[] args) { FileInfo info = new FileInfo(&quot;D:/a.txt&quot;); FileInfo info1 = new FileInfo(&quot;D:/b.bat&quot;); StreamReader sr = info.OpenText(); StreamWriter sw = new StreamWriter(&quot;D:/b.bat&quot;,false,Encoding.Unicode);//info.AppendText(); //info.CreateText(); string result = null; do { result = sr.ReadLine(); sw.WriteLine(result); } while (result != null); sr.Close(); sw.Close(); } }</textarea>

TextReader,TextWriter --表示可读取连续字符系列的读取器。TextReader 为 StreamReader 和 StringReader 的抽象基类,它们分别从流和字符串读取字符。使用这些派生类可打开一个文本文件以读取指定范围的字符,或基于现有的流创建一个读取器。

<textarea cols="50" rows="15" name="code" class="c-sharp">class TextRW { static void Main() { TextWriter stringWriter = new StringWriter(); using(TextWriter streamWriter = new StreamWriter(&quot;InvalidPathChars.txt&quot;)) { WriteText(stringWriter); WriteText(streamWriter); } TextReader stringReader = new StringReader(stringWriter.ToString()); using(TextReader streamReader = new StreamReader(&quot;InvalidPathChars.txt&quot;)) { ReadText(stringReader); ReadText(streamReader); } } static void WriteText(TextWriter textWriter) { textWriter.Write(&quot;Invalid file path characters are: &quot;); textWriter.Write(Path.InvalidPathChars); textWriter.Write('.'); } static void ReadText(TextReader textReader) { Console.WriteLine(&quot;From {0} - {1}&quot;, textReader.GetType().Name, textReader.ReadToEnd()); } } </textarea>

MemoryStream --创建其支持存储区为内存的流。 MemoryStream 类创建这样的流,该流以内存而不是磁盘或网络连接作为支持存储区。MemoryStream 封装以无符号字节数组形式存储的数据,该数组在创建 MemoryStream 对象时被初始化,或者该数组可创建为空数组。可在内存中直接访问这些封装的数据。内存流可降低应用程序中对临时缓冲区和临时文件的需要。

<textarea cols="50" rows="15" name="code" class="c-sharp">class MemStream { static void Main() { int count; byte[] byteArray; char[] charArray; UnicodeEncoding uniEncoding = new UnicodeEncoding(); // Create the data to write to the stream. byte[] firstString = uniEncoding.GetBytes( &quot;Invalid file path characters are: &quot;); byte[] secondString = uniEncoding.GetBytes( Path.InvalidPathChars); using(MemoryStream memStream = new MemoryStream(100)) { // Write the first string to the stream. memStream.Write(firstString, 0 , firstString.Length); // Write the second string to the stream, byte by byte. count = 0; while(count &lt; secondString.Length) { memStream.WriteByte(secondString[count++]); } // Write the stream properties to the console. Console.WriteLine( &quot;Capacity = {0}, Length = {1}, Position = {2}/n&quot;, memStream.Capacity.ToString(), memStream.Length.ToString(), memStream.Position.ToString()); // Set the position to the beginning of the stream. memStream.Seek(0, SeekOrigin.Begin); // Read the first 20 bytes from the stream. byteArray = new byte[memStream.Length]; count = memStream.Read(byteArray, 0, 20); // Read the remaining bytes, byte by byte. while(count &lt; memStream.Length) { byteArray[count++] = Convert.ToByte(memStream.ReadByte()); } // Decode the byte array into a char array // and write it to the console. charArray = new char[uniEncoding.GetCharCount( byteArray, 0, count)]; uniEncoding.GetDecoder().GetChars( byteArray, 0, count, charArray, 0); Console.WriteLine(charArray); } } }</textarea>

异步IO --不解释,看代码

<textarea cols="50" rows="15" name="code" class="c-sharp">class Program { static void Main(string[] args) { new AsyncReader().Read(); for (int i = 0; i &lt; 100000; i++) if (i % 10000 == 0) Console.WriteLine(i); } } public class AsyncReader { public static readonly string FILENAME = &quot;D:/a.txt&quot;; public static readonly int BUFFER_SIZE = 128; public byte[] buffer_array = new byte[BUFFER_SIZE]; public Stream stream; public void Read() { stream = new FileInfo(FILENAME).OpenRead(); stream.BeginRead(buffer_array, 0, BUFFER_SIZE, OnReadCompleted, null); } private void OnReadCompleted(IAsyncResult result) { int bytesRead = stream.EndRead(result); if (bytesRead &gt; 0) { string s = Encoding.ASCII.GetString(buffer_array,0,bytesRead); Console.WriteLine(s); stream.BeginRead(buffer_array,0,BUFFER_SIZE,OnReadCompleted,null); } } }</textarea>

网页流--代码

<textarea cols="50" rows="15" name="code" class="c-sharp"> class Program { static void Main(string[] args) { //WebRequest is a factory,when param is a uri,return the HttpWebRequest sub class. //create a connection to the website.what you get back from the host is encapsulate in the HttpWebRequest object. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(&quot;http://www.baidu.com&quot;); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader( response.GetResponseStream(),Encoding.ASCII ); string content = reader.ReadToEnd(); Console.WriteLine(content); } }</textarea>

 

 

你可能感兴趣的:(流(2)--流)