How do you usually read the files? Use stream read. Yes, C # provides us with a very powerful class library (once again touted.) NET, which encapsulates almost everything we can think of and we do not think of class, stream is the general means of reading files, then you will really use it to read the data in the file? Can you really read the whole thing?
Usually we read a file using the following steps:
1. Declare and instantiate a file stream object using the OpenRead of file, as follows
2. Prepare a byte array containing the contents of the file, FS. Length will get the actual size of the file, just like the following
byte[] data = new BYTE[FS. Length];
3, Wow! Start reading, a method that invokes a file stream reads the data into an array
Fs. Read (data, 0, data.) Length);
Oh! We have only written 3 words can be the contents of the document is read out, it is too concise! Can this code really work as you expected? The answer is: almost! In most cases the above code works very well, but we should note that the Read method has a return value, and since there is a return value, there must be a reason, if you follow the above can be a function of no return value. The purpose of the return value is to give us an opportunity to determine the size of the actual read file, and to determine whether the file has been fully read. So the code above does not guarantee that we must have read all the bytes in the file (although in many cases it has been read). The following method provides a more secure method than the previous method to ensure that the file is read out completely
public static void Saferead (Stream stream, byte[] data) {
int offset=0;
int remaining = data. Length;
Read the rest of the bytes as long as they are available.
while (Remaining > 0) {
int read = stream. Read (data, offset, remaining);
In some cases you do not know the actual length of the stream, such as network flow. A similar method can be used to read the stream until the data inside the stream is fully read. We can initialize a cache, and then write the stream that is read to the memory stream, as follows:
public static byte[] Readfully (Stream stream) {
Initializes a 32k cache
byte[] buffer = new byte[32768];
using (MemoryStream ms = new MemoryStream ()) {//return result automatically reclaims the Dispose method that calls the object to free memory
Keep reading.
while (true) {
int read = stream. Read (buffer, 0, buffer.) Length);
You can return the results until you've read the last 3M data.
if (read <= 0)
Return Ms. ToArray ();
Ms. Write (buffer, 0, read);
}
}
}
Although the above examples are relatively simple, the effect is not very obvious (most of them are right), maybe you would have, it's okay this article is originally written to beginners.
The following method provides a way to read a stream using the specified cache length, although in many cases you can get the length of the stream directly using Stream.length, but not all streams can be obtained.
public static byte[] Read2buffer (Stream stream, int bufferlen) {
Specifies a default length as the cache size, if a buffer of the specified length is invalid
if (Bufferlen < 1) {
Bufferlen = 0x8000;
}
Initialize a buffer
byte[] buffer = new Byte[bufferlen];
int read=0;
int block;
Every time you read the cached data from the stream, you know that all the streams are read
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.