關於C#中的StreamReader與FileStream這兩個類

來源:互聯網
上載者:User

□簡介StreamReader
以Stream為服務中心。那麼這個stream一定是檔案的stream嗎?不一定,可能是檔案的,也可能是其它的,比如從HttpWebResponse轉化而來的Stream。

建構函式的兩大類
1從stream中讀取
StreamReader(Stream) Initializes a new instance of the StreamReader class for the specified stream.

2從File中讀取
StreamReader(String) Initializes a new instance of the StreamReader class for the specified file name.

成員函數
1. Peek() 
Returns the next available character but does not consume it.
2. Read() 
Reads the next character from the input stream and advances the character position by one character.
3. Read(Char[], Int32, Int32) 
Reads a specified maximum of characters from the current stream into a buffer, beginning at the specified index.
4. ReadBlock() 
Reads a maximum of count characters from the current stream, and writes the data to buffer, beginning at index.
5. ReadLine() 
Reads a line of characters from the current stream and returns the data as a string.
6. ReadToEnd() 
Reads the stream from the current position to the end of the stream.

StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading

lines of information from a standard text file.

□簡介 FileStream
是以檔案為服務中心的。Exposes a Stream around a file

□FileStream的write()
是把byte[]寫入到Stream中
public override void Write(byte[] array,int offset,int count)
array
 Type: System.Byte[]
 The buffer containing data to write to the stream.
offset是源頭的位移量
 Type: System.Int32
 The zero-based byte offset in array at which to begin copying bytes to the current stream.
count
 Type: System.Int32
 The number of bytes to be written to the current stream
 return value 無傳回值

□例 FileStream的read()
是把Stream讀入到byte[]數組中
public override int Read(byte[] array,int offset,int count)
array
 Type: System.Byte[]
 When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current

 source.
offset 是目標的位移量
 Type: System.Int32
 The byte offset in array at which the read bytes will be placed.
count
 Type: System.Int32
 The maximum number of bytes to read.
Return Value
 Type: System.Int32
 The total number of bytes read into the buffer. This might be less than the number of bytes requested if that number of bytes are not currently available, or  

zero if the end of the stream is reached.
小結:
不管是FileStream的read()還是write()操作,offset這個參數永遠只是指byte[]數組中的位移量。具體地說,在read()中,offset指的是目標的位移量,在write()中,offset指的是源的位移量。

 


FileStream fsSource = new FileStream(pathSource,FileMode.Open, FileAccess.Read)
byte[] bytes = new byte[fsSource.Length];
int numBytesToRead = (int)fsSource.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
 // 注意這裡的anything這個說法很重要:Read may return anything from 0 to numBytesToRead返回的位元組數可能為任意比numBytesToRead小的數。
int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);
// Break when the end of the file is reached.
if (n == 0)
break;
numBytesRead += n;
numBytesToRead -= n;
}

 

□還有哪些Stream?
BufferedStream 

FileStream 

MemoryStream 

UnmanagedMemoryStream 

Stream

StreamReader

StreamWriter

□還有哪些reader和writer?
BinaryReader 

Reads primitive data types as binary values in a specific encoding.
BinaryWriter 

Writes primitive types in binary to a stream and supports writing strings in a specific

encoding.

TextReader 

Represents a reader that can read a sequential series of characters.
TextWriter 

Represents a writer that can write a sequential series of characters. This class is abstract.
StringReader 

Implements a TextReader that reads from a string.
StringWriter 

Implements a TextWriter for writing information to a string. The information is stored in an underlying StringBuilder.
StreamReader 

Implements a TextReader that reads characters from a byte stream in a particular encoding.
StreamWriter 

Implements a TextWriter for writing characters to a stream in a particular encoding.

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.