C # Blog Eighth week

Source: Internet
Author: User

IO operations basically need to use the stream-related subclasses, so this kind of problem in Csdn asked more. In fact, for the stream, the operation is relatively simple, as long as the details of the treatment of a little attention, I believe in the use of it will be handy. For a stream-related issue, the following categories are broadly divided. Problem one, basic operation problem, problem two, coding problem, problem three, tail processing problem, problem four, stream cache problem, problem five, resource release problem, last question, how to use stream to update large file part data.
For problem one, the basic operation of the problem, mainly reading and writing problems, mainly appear in the file data is relatively large, need to cycle to write or read the time. The correct reading at this time is as follows.

using (FileStream fs = new FileStream (Yourfile, Filemode.open,fileaccess.read,fileshare.none))

Filemode.open,fileaccess.read,fileshare.none))

{int nrealread = 0;

byte[] Bbuffer = new byte[1024];

Do

Nrealread = fs. Read (bbuffer, 0, 1024);

Debug.WriteLine (Encoding.Default.GetString (bbuffer, 0, Nrealread));

}while (Nrealread = = 1024);

}

But when most people do this for the first time, they will be in "Nrealread = fs." Read (bbuffer, 0, 1024); " This sentence makes a mistake. It is assumed that the offset of the second parameter is set for the stream, so it is assumed that the cumulative value, that is, the total number of bytes currently read. There is a need to understand the operation of the stream, and when it is read or written, the stream's cursor will be automatically followed by the bytes read or written. Next Stream.read or stream.write the second parameter in the two methods is for the first buffer parameter, not for the stream, so do not make a mistake in this place.
The basic question also involves the way the file is opened. Some people often ask how to open the same file with two streams at the same time. In fact, the default stream open mode is exclusive, so when the file is not specified for access to share, after opening the file operation will be an exception, so you need to write to me above.       Also, if you need to specify the starting position of the current stream, you can do so either by the Seek method or by setting the Position property. For issue two, the encoding problem. Some people use the subclass of stream, such as StreamReader to open a text file, found that the data read is garbled, resulting in most of this reason because the file contains Chinese characters, while opening the file without specifying the encoding method. Because of the different encoding methods in English and Chinese, it is sometimes caused to read Chinese errors when the encoding is not specified.       Just use the constructor with the Encoding parameter in the StreamReader type, for example: using (StreamReader sr = new StreamReader (Yourfile, Encoding.default))        This only uses the system default encoding, but it may not be suitable for your file encoding, so you need to debug and transform this parameter in practical application. The third problem is the stream tail processing problem. This type of problem shows that the file will grow when copying files. So when using Stream.read and Stream.Write, the return value of the method is used to indicate the number of bytes actually read and written, as previously written.

Nrealread = fs.  Read (bbuffer, 0, 1024); Read Data

Debug.WriteLine (Encoding.Default.GetString (bbuffer, 0, Nrealread));

At this time, the output is not "1024", but "nrealread" as a valid byte indicator.

For issue four, the stream caches the problem, which is mainly manifested at the time of writing. In order to avoid frequent operation of IO and reduce efficiency, most streams use asynchronous write, that is, the stream object to be equipped with a certain cache, to temporarily save the written data. However, the cache is limited, and when the cache is full it causes subsequent write data to not be written, resulting in data loss. Then you need to display the call Stream.flush method to write the cached data to the file and empty the cache. In fact, this is not the only way, in some of the stream's subclasses also provides a way to set buffersize, or provide a set of autoflush properties for automatic writing, and so on, so here you can choose different methods to complete according to different needs.
        for stream release, this is probably not just a matter of using stream, it may be a bad habit of using C # programming. While C # resources are managed, the stream, if not released in a timely manner, can cause an inability to open when other threads or processes use the file (since most of the stream is opened exclusively) and does not close in time. The buffer occupied cannot be released in time. So it's important to develop a good habit. Actually releasing the stream is simple, either showing the call to its close and dispose methods, or using a using program block, as I wrote earlier.        the last one is how to use stream to update large files. It is common to think that when a file is larger, but there are few parts that need to be modified, you want to do something like delete, insert, or replace directly in a location via stream. For a file update operation, roughly divided into three kinds, here is mainly to consider the location of the update and update the length of the data.       first for file tail extension operation, content length is not limited to;      the second kind of equal byte substitution operation, the location is not limited;       the last one is the position is not fixed, the number of bytes is uncertain.       the first two of the above mentioned, the processing is relatively simple. For the first type, add append when setting FileMode. For the substitution of equal bytes, it is simpler to find the specified position directly through Stream.seek, and then call Stream.Write.         and the last one, is the most troublesome. A simple solution, create a temporary file, and then read one side of the writing, encountered the need to modify, read it first and then modify the last write. When all finished, delete the old file, modify the name of the temporary file is the original name. The more troublesome solution is to manipulate the source files directly using a read stream and a write stream through the share method. It is important to note that in order to ensure that the newly written data does not go out of the data that has not been read, that is to control the writing stream to write the location of not more than the location to read. For example, the current location to read is the file's800 bytes, that is, 800 bytes have not been read out of processing, at this time write stream after writing the data, stream location can not exceed 800 bytes, if the write is cached, then more than 800 bits of data do not immediately through flush to submit. In general, using two streams to operate the same file, pay special attention to this point, the processing is not good to cause a dead loop. The problem with        stream is relatively simple, and most people do not pay attention to details when they operate. So I'm just a little bit of clarification, not a very detailed description.

C # blog Eighth week

Related Article

Contact Us

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.

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.