Stream when you use vb.net to read and write files for the first time, you will surely find that vb.net discard traditional file I/O support and feel unaccustomed. In fact, in. NET, Microsoft has replaced traditional file operations with rich "streaming" objects, and "streaming" is an object that is often used in Unix.
We can think of a stream as a channel through which data can be "streamed" to various data storage institutions (such as files, strings, arrays, or other forms of flow). Why do we discard the long IO operation and the stream? One of the important reasons is that not all data exists in the file. Now, the program takes data from various types of data stores, such as a file, a buffer in memory, and the Internet. Streaming technology allows an application to get a variety of data based on a programming model without having to learn how to get the specific technology of a file on a remote Web server. We just need to create a stream between the application and the Web server, and then read the data sent by the server.
Stream objects, encapsulating the various operations of reading and writing data sources, the biggest advantage is that when you learn how to operate a data source, you can extend this technology to other kinds of data sources.
Type of flow
A stream is an abstract class, and you cannot declare an instance of the stream in a program. Within. NET, 5 specific streams are derived from the stream, respectively:
FileStream supports sequential and random read and write operations on files
MemoryStream supports sequential and random read-write operations on memory buffers
NetworkStream supports sequential and random read and write operations on Internet network resources, existing in System.Net.Sockets namespaces
CryptoStream supports encoding and decoding of data, existing in System.Security.Cryptography namespaces
BufferedStream supports buffered read-write to objects that are not supported by themselves
Not all streams take the same approach, such as reading a stream of local files, you can tell us the length of the file, the current read-write location, and so on, you can use the Seek method to jump to any location in the file. In contrast, streams that read remote files do not support these attributes. However, the stream itself has CanSeek, CanRead, and CanWrite attributes that distinguish the data source and tell us whether to support or not support an attribute.
Here we briefly introduce a FileStream class
FileStream class
In the local file operation, we can use the Filesteam class, can be very simple read and write as a byte array (arrays of bytes). For reading and writing data of simple data types, you can use BinaryReader and BinaryWriter as well as streamreader,streamwriter classes. BinaryReader, a primitive data type is read as a binary value with a specific encoding. BinaryWriter writes a primitive type to the stream in binary form and supports writing a string with a specific encoding. Streamreader/writer stores the data in XML format. There is little difference in vb.net, because the classes used are applied to both formats.
Vb. NET supports traditional random read and write files, you can create files for storing struct, and then access them according to the number of records. As in previous versions of VB, use the Fileopen,fileget function. To a large extent, this has been replaced by XML or a database. If you create a new application and there is no need to consider compatibility issues with the version, it is recommended. NET's new features.
Whatever you are going to use to get a streamclass, you have to create a FileStream object. There are many ways to create, the simplest is to specify the file path, open the mode, such as the following syntax.
Dim FStream as New FileStream (path, FileMode, FileAccess)
Path to include file paths and file names. FileMode is one of the members of the enumeration type FileMode, as shown in the following table. FileAccess is a member of the enumeration type FileAccess. Read (read only), ReadWrite (read-write), and write (writes). Determines the read and write permissions of the file.
Member name
Description
Append
Open an existing file and find the end of the file, or create a new file.
Create
Specifies that the operating system should create a new file. If the file already exists, it will be overwritten.
CreateNew
Specifies that the operating system should create a new file.
Open
Specifies that the operating system should open an existing file.
OpenOrCreate
Specifies that the operating system should open the file (if the file exists), otherwise, create a new file.
Truncate
Specifies that the operating system should open an existing file. Once the file is opened, it will be truncated to a size of 0 bytes.
Of course, you can also use (Open, OpenRead, OpenText, Openwrite) to create FileStream
Dim FS as New FileStream = IO. File.openwrite ("C:\Stream.txt")
Another way to open a file is to use the OpenFile method of the OpenFileDialog and SaveFileDialog controls.
You do not need to specify any parameters. The OpenFileDialog OpenFile method opens the file in read-only mode; The SaveFileDialog OpenFile method opens the file in read-write mode.
FileStream supports only the most basic operations, writing data to a byte array or writing to a file from a byte array. If we use FileStream to save the data in a file, we first convert the data to a byte array, and then call the FileStream write method. Similarly, the FileStream read method returns a byte array. You may not always use FileStream objects directly, we still need to take a look at its basic functions
After you create the FileStream object, call WriteByte to write a byte into the file. The Write method can write an array to a file, requiring three parameters
Write (buffer, offset, count)
Buffer is to write to the array address, offset is offset, count refers to the number of bytes written, read syntax is the same.
Since FileStream to deal with bytes array, it is necessary to study asciiencoding GetBytes and UnicodeEncoding GetChars.
The following example is a conversion operation.
Dim buffer () as Byte
Dim Encoder as New System.Text.ASCIIEncoding ()
Dim str as String = "This are a line of text"
ReDim buffer (str. LENGTH-1)
Encoder.getbytes (str, 0, str.) Length, buffer, 0)
Fs. Write (buffer, 0, buffer.) Length)
Note: You must resize the byte array to write to read-write length.
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.