Grasp the stream in VB.NET (stream) (1)
When you first read and write files with VB.NET, you will definitely find that VB.NET has abandoned traditional file I / O support and feels not used to it. In fact, in .NET, Microsoft replaces traditional file operations with rich "flow" objects, and "flow" is an object that is often used in UNIX.
We can treat the stream as a channel, the data of the program can be "stream" to various data storage mechanisms along this channel (such as files, strings, arrays, or other forms). Why do we abandon your IO operations, and the generation is current? One of the most important reasons is not all the data exists in the file. Now programs, get data from various types of data stores, such as a file, buffer in memory, and Internet. Streaming technology allows applications to obtain various data based on a programming model, without having to learn how to get specific techniques for a file on the remote web server. We only need to create a stream between the application and the web server, and then read the data sent by the server.
Streaming objects, various operations that encapsulate the read and write data sources, the biggest advantage is that when you do a certain data source, you can extend this technique to other shapes of data sources.
Cyber
The stream is an abstract class that you cannot declare a instance of Stream in the program. In .NET, STREAM is derived from 5 specific streams, respectively.
FILESTREAM supports the order and random reading of files
MemoryStream supports the order and random reading of memory buffers
NetworkStream supports the order and random read and write operations of Internet network resources, existing in system.net.sockets namespace
CryptostReam supports data encoding and decoding, exists in system.security.cryptography namespace
BufferedStream supports buffer reading and writing to objects that do not support itself
Not all stream uses the same method as fully touching, such as the stream of the local file, can tell us the length of the file, the location of the current read and write, etc. You can use the Seek method to jump to the file. Instead, these features are not supported by reading the stream of remote files. However, Stream itself has the Canseek, Canread and CanWrite properties for distinguishing data sources, telling us that support is still in a certain characteristic.
Below we briefly introduce a FileStream class
FILESTREAM class
When performing a local file operation, we can use the FILESTEAM class, you can read and write as an arrays of bytes. For simple data types of data read and write, BinaryReader and BinaryWriter and StreamReader, Streamwriter class can be used. BinaryReader, read the primary data type by a specific code to binary values. BinaryWriter writes the current type in binary form to stream and supports a specific encoding write string. StreamReader / Writer is stored in XML format. The difference is not large in VB.NET, because the classes used are applied to two formats.
VB.NET supports traditional random read and write files, you can create files for storing Struct and then access according to record number. Just like in previous VB versions, use FileOpen, Fileget functions. To a large extent, this has been replaced by XML or database. If you create a new application, there is no need to consider compatibility with the version, it is recommended to use the new features of .NET. Whether you have to use a streamclass, you must create a FILESTREAM object. There are many ways to create, the simplest is to specify the file path, open the mode, such as the syntax below.
DIM FSTREAM As New FileStream (Path, FileMode, FileAccess)
Path To include the path of the file and the file name. FileMode is one of the members of the enumeration type FileMode, as shown in the following table. FileAccess is a member of enumeration type FileAccess. Readwrite, READWRITE (Write), and WRITE. Decided to read and write the file.
Member name
Description
Append
Open existing files and find the file end, or create a new file.
Create
Specifies the operating system to create a new file. If the file already exists, it will be rewritten.
CreateNew
Specifies the operating system to create a new file.
Open
Specifies the operating system to open an existing file.
Openorcreate
Specifies the operating system to open the file (if the file exists); otherwise, a new file should be created.
Truncate
Specifies the operating system to open an existing file. Once the file is opened, it will be truncated to zero byte size.
Of course, you can also create fileStreams (Open, OpenRead, OpenText, OpenWrite).
DIM FS AS New FileStream = IO.File.OpenWrite ("c: /stream.txt")
Another way to open the file with the OpenFile method of the OpenFileDialog and SaveFileDialog control.
You don't need to specify any parameters. OpenFileDialog's OpenFile method opens a file in read-only mode; SaveFileDialog's OpenFile method opens files in reading and writing.
FileStream only supports the most basic operations, writing data to byte arrays or writes from byte arrays. If we save the data in a file with FileStream, you first convert the data to a BYTE array, then call the FileStream's Write method. Similarly, the FileStream's Read method returns the byte array. You may not always use the FileStream object directly, we still need to simply look at its basic function
After creating a FileStream object, call WRITEBYTE to write a byte into the file. Write method can write an array to files and require three parameters
Write (buffer, offset, count)
Buffer is to write an array address, offset is an offset, and count refers to the number of bytes, the syntax of the read.
Since FileStream is to deal with bytes array, study ASCIENCODING Gettes and UnicodeEncoding getchars must be necessary
The following example is a conversion operation.
Dim buffer () AS BYTE
DIM Encoder as new system.text.asciiencoding ()
DIM STR As String = "this is a line of text"
Redim buffer (str.length - 1)
Encoder.getbytes (Str, 0, Str.length, Buffer, 0)
Fs.write (buffer, 0, buffer.length)
Note: The BYTE array to be written to be written is the length of the read and write.