Read and write code for asp.net FileStream

Source: Internet
Author: User
Tags array length

Asp tutorial. net filestream read and write code

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 can open an existing file and find the end of the file. filemode. append can only be used with fileacces. write (write permission).
Create creates a new file. If the file exists, it overwrites the file. If not, it creates a new file.
Createnew creates a new file. If yes, an exception occurs.
Open the existing file. If yes, an exception occurs if no file exists.
If a file exists, the file is opened. If the file exists, the file is opened. If the file does not exist, the file 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 );

 

Define a byte array, set the length of the array, write bytes to the byte array cyclically, and write the values in the array from 0 to the stream through the write method;

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.