In the above content, we will show you how to open a file. Next we will look at how to read and write an opened file.
The first is read operations. The filestream class defines two methods for reading bytes from a file: Read () and readbyte (). readbyte () is used to read one byte, read () is to read a byte block.
When we define a filestream reference object, if it does not point to any object, we can initialize it to null. The advantage of this is that if we need to close a data stream object at last, if the data stream object is empty, an exception occurs when you disable it. By initializing it to null, we can use the if statement to determine whether the data stream exists. If yes, I used the close () method to close it.
The following describes a two-byte function.
The general form of readbyte is:
Int readbyte ()
During the call, a byte is read from the file and an integer is returned. If the value reaches the end of the file,-1 is returned.
The general form of read () is:
Int read (byte [] array, int offset, int count)
This method reads count bytes from the data stream sequentially and writes these bytes to the array from array [offset]. The total number of successfully read bytes is returned.
Compared with reading bytes, writing bytes to the file exists. There are two file writing functions corresponding to the reading function above. They are the write () and writebyte () methods respectively. The two functions are described as follows:
Void writebyte (byte value)
This method writes the specified byte of value to the file. The following method writes a byte array to the file. The prototype of this method is as follows:
Void write (byte [] array, int offset, int count );
This method writes count bytes from the position of the subscript offset in the array to the file.
If the underlying data stream is not enabled for output, a notsupportedexception exception is thrown. If the data stream is disabled, an objectdisposedexception exception is thrown.
Note that when executing the file output, the output content is not directly written to the real physical device, and the operating system will save the output content to the buffer first, when the buffer zone is full, a large amount of data is written at a time. Of course, if we want to write data to the physical device before the buffer is full, we need to use the flush () function.
When processing the output file, you must use the close () method to close the file.
We have discussed how to read and write files in bytes. However, we are more accustomed to character-based file operations. In this case, we need to convert byte data streams into character data streams. The following describes two character data stream types used for file operations.
The streamreader class is derived from the textreader class and is a character-based data stream type. When we need to create a character-based Data Stream object, we need to wrap the data stream into a streamreader object. In this case, we need to use a common constructor, as shown below:
Streamreader (Stream stream );
This constructor uses a data stream object as a parameter. If the parameter is null, an argumentnullexception exception is thrown. If the data stream specified by stream is not opened for reading, an argumentexception exception is thrown.
We also have a simple way to open a file. In this case, we need to use another common constructor:
Streamreader (string path );
The parameter path specifies the name of the file to be opened (including the file name and complete file path information ).
Here, we add that the streamreader class provides the endofstream attribute to check whether it has reached the end of the data stream. When it reaches the end, this attribute is true.
In contrast to read operations, the streamwriter class implements character-based write operations, which are derived from the textwriter class.
The streamwriter class also has two common constructor functions, as shown below:
Streamwriter (Stream stream );
Streamwriter (string path );
Streamwriter (string path, bool append );
The first is to wrap a stream object in the streamwriter class; the second is to open the file specified by the path parameter for write operations; the third is similar to the second, the append parameter specifies whether to append the written content to the end of the file. If the append parameter is true, the input content is appended to the end of the file.