Use
FilestreamClass to read, write, open, and close files in the file system, and operate other file-related operating system handles, such as pipelines, standard inputs, and standard outputs. Read/write operations can be specified as synchronous or asynchronous operations.
FilestreamBuffer input and output to improve performance.
Let's take a look at the code, which will be explained later:
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. IO; // note that
Namespace wasteindustry {
Public partial class form3: FORM {
Public form3 (){
Initializecomponent ();
/*
* C # Program
* Author: shenzhenlong
* Email: hxl2008hxl@hotmai.com
*/
}
Private void form3_load (Object sender, eventargs e ){
// Create a text file and instantiate a file stream
Filestream myfilestream1 = new filestream (@ "D: \ test. Text", filemode. Create );
Fileinfo myfiles = new fileinfo (@ "d; \ test. Text ");
// Instantiate a read-only file stream
Filestream myfilestream2 = myfiles. openread ();
// Set the file stream to the write-only permission
Myfilestream2 = myfiles. openwrite ();
// Set the file stream to append, read-only, not shared
Myfilestream2 = myfiles. Open (filemode. append, fileaccess. Read, fileshare. None );
// Create a file
Myfilestream2 = myfiles. Create ();
// Read bytes from the file
Int mybytes = myfilestream1.readbyte ();
// Define the array Length
Int numberofbytes = 20;
// Byte array
Byte [] mybytearray = new byte [numberofbytes];
// Read 20 bytes from the file and put them in the array
Int bytesread = myfilestream1.read (mybytearray, 0, numberofbytes );
// Define a byte
Byte mywritebyte = 100;
// Write a byte to the file
Myfilestream1.writebyte (mywritebyte );
// Define the array Length
Int numberofbytestowrite = 256;
// Define byte array
Byte [] mywritebytearray = new byte [numberofbytestowrite];
// Insert the byte value to the byte array cyclically
For (INT I = 0; I <256; I ++ ){
Mywritebytearray [I] = (byte) I;
I ++;
}
// Write the array content to the stream
Myfilestream1.write (mywritebytearray, 0, numberofbytestowrite );
// Close the stream
Myfilestream1.close ();
// Close the stream
Myfilestream2.close ();
}
}
}
The myfilestream1 object is instantiated using the specified path and creation mode. The Creation Mode filemode is an enumeration type and also includes:
| Append |
You can open an existing file and find the end of the file. filemode. append can only be used with fileacces. Write (write permission ). |
| Create |
Create a new file. If the file exists, overwrite it. If no, create a new file. |
| Createnew |
Create a new file. If yes, an exception occurs. |
| Open |
Open an existing file. If yes, an exception occurs if no file exists. |
| If a file exists, it is opened. If the file does not exist, it is created. |
If a file exists, it is opened. If the file does not exist, it is created. |
|
|
The myfilestream2 object is instantiated using fileinfo. The fileinfo methods include open, openreade, opentext, create, and createtext.
Reading and writing of file streams );
Read
// Define the array Length
Int numberofbytes = 20;
// Byte array
Byte [] mybytearray = new byte [numberofbytes];
// Read 20 bytes from the file and put them in the array
Int bytesread = myfilestream1.read (mybytearray, 0, numberofbytes );
Define an array of the byte type and set the length of the array. The read method is to read the array from 0, read 20 bytes, and replace it with the array.
// Define the array Length
Int numberofbytestowrite = 256;
// Define byte array
Byte [] mywritebytearray = new byte [numberofbytestowrite];
// Insert the byte value to the byte array cyclically
For (INT I = 0; I <256; I ++ ){
Mywritebytearray [I] = (byte) I;
I ++;
}
// Write the array content to the stream
Myfilestream1.write (mywritebytearray, 0, numberofbytestowrite); defines an array of the byte type, sets the length of the array, and writes bytes to the byte array through a loop, use the write method to write the value in the array from 0 to the stream;
From: http://www.cnblogs.com/shenzhoulong/archive/2011/02/16/1956530.html