Skip to main content
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Stream.ReadByte Method
- Reference
Definition
In this article
Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.
public:
virtual int ReadByte[];
public virtual int ReadByte [];
abstract member ReadByte : unit -> int
override this.ReadByte : unit -> int
Public Overridable Function ReadByte [] As Integer
Returns
Int32The unsigned byte cast to an Int32, or -1 if at the end of the stream.
Exceptions
The stream does not support reading.
Methods were called after the stream was closed.
Remarks
Use the CanRead property to determine whether the current instance supports reading.
Attempts to manipulate the stream after the stream has been closed could throw an ObjectDisposedException.
Notes to Inheritors
The default implementation on Stream
creates a new single-byte array and then calls
Read[Byte[], Int32, Int32]. While this is formally correct, it is inefficient. Any stream with an internal buffer should override this method and provide a much more efficient version that reads the buffer directly, avoiding the extra array allocation on every call.
Applies to
See also
- File and Stream I/O
- How to: Read Text from a File
- How to: Write Text to a File
- Các stream
- Làm việc với file stream
- Lấy encoding của file
- Ghi file text bằng stream
- Đọc file text bằng stream
- Copy file bằng stream
Khái niệm về stream
Một luồng [stream] là một đối tượng được sử dụng để truyền dữ liệu. Khi dữ liệu truyền từ các nguồn bên ngoài vào ứng dụng ta gọi đó là đọc stream
, và khi dữ liệu truyền từ chương trình ra nguồn bên ngoài ta gọi nó là ghi stream
.
Nguồn bên ngoài thường là các file, tuy nhiên tổng quát thì nó có thể từ nhiều loại như đọc/ghi dữ liệu thông qua một giao thức mạng để trao đổi dữ liệu với một máy remote khác, đọc/ghi vào một nhớ ...
Một số stream chỉ cho đọc, một số chỉ cho ghi, một số lại cho phép truy cập nhẫu nhiên chứ không chỉ truy cập tuần tự [cho phép thay đổi vị trí con trỏ đọc dữ liệu trong luồng - ví dụ dịch chuyển vào giữa luồng dữ liệu để đọc dữ liệu ở khoảng nào đó]
Thư viện .NET cung cấp lớp cơ sở System.IO.Stream
để hỗ trợ làm việc đọc ghi các byte dữ liệu với các stream, từ lớp cơ sở này một loạt lớp kế thừa cho những stream đặc thù như: FileStream
, BufferStream
, MemoryStream
...
Lấy thông tin về
stream - khi có đối tượng lớp System.IO.Stream
có một số thuộc tính để tra cứu thông tin về stream
CanRead
| Cho biết stream hỗ trợ việc đọc hay không |
CanWrite
| Cho biết stream hỗ trợ việc ghi hay không |
CanSeek
| Cho biết stream hỗ trợ dịch chuyển con trỏ hay không |
CanTimeout
| Cho biết stream có đặt được time out |
Length
| Cho biết kích thước [byte] của stream |
Position
| Đọc hoặc thiết lập vị trí đọc [thiết lập thì stream phải hỗ trợ Seek] |
ReadTimeout
| Đọc hoặc thiết lập giá trị [mili giây] danh cho tác vụ đọc stream trước khi time out phát sinh |
WriteTimeout
| Đọc hoặc thiết lập giá trị [mili giây] danh cho tác vụ ghi stream trước khi time out phát sinh |
Một số phương thức cho đối tượng Stream
ReadByte
| Đọc từng byte, trả về int [cast 1 byte] hoặc -1 nếu cuối file. |
Read
| Đọc một số byte, từ vị trí nào đó, kết quả đọc lưu vào mảng byte. Trả về số lượng byte đọc được, 0 nếu cuối stream. // Tạo một stream và lưu vào đó một số byte Stream stream = new MemoryStream []; for [int i = 0; i < 122; i++] { stream.WriteByte [[byte] i]; } // Thiết lập vị trí là điểm bắt đầu stream.Position = 0; // Đọc từng block 5 bytes byte[] buffer = new byte[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // mảng chứa 10 byte để làm bộ nhớ lưu byte đọc được int offset = 0; // vị trí [index] trong buffer - nơi ghi byte đầu tiên đọc được int count = 5; // đọc 5 byte int numberbyte = stream.Read [buffer, 0, 2]; // bắt đầu đọc while [numberbyte != 0] { Console.Write [$"{numberbyte} bytes: "]; for [int i = 1; i |