Use memorystream and filestream

Source: Internet
Author: User
Programming access files are carried out through file stream objects, when the application Program To access a file, you must first create a file stream object, which corresponds to the file.
In. net, the abstract base class system. Io. stream is used to represent the stream. It provides two methods: read and write. Due to the orderliness of the data stream, the stream object also has a read/write pointer. For this reason, the stream class also has a seek Method for moving read/write pointers.
The data of the filestream object comes from the file, while that of the memorystream object comes from the memory buffer. Both classes are inherited from the stream class.

The data of memorystream comes from a contiguous area in the memory. This area is called "buffer )". You can regard the buffer zone as an array. Each array element can store one byte of data.

When creating a memorystream object, you can specify the buffer size and change it as needed. // Byte array
Byte [] Buffer =   New   Byte [ 600 ];
// Fill byte array
Private   Void Createexampledata ()
{
For ( Int I = 0 ; I < 600 ; I ++ )
{< br> // the maximum number of bytes cannot exceed 255, use 256 modulo implementation
buffer [I] = ( byte ) (I % 256 );
}
}

Basic usage of memory stream: Private   Void Ontestmemory ()
{
// Create Test Data
Createexampledata ();

// Creates a memory stream object and initially allocates a buffer of 50 bytes.
Memorystream mem =   New Memorystream ( 50 );

// Write all data in the byte array into the memory stream
Mem. Write (buffer, 0 , Buffer. getlength ( 0 ));

MessageBox. Show ( " Memory stream length after data writing: "   + Mem. length. tostring ());
MessageBox. Show ( " The buffer size allocated to the memory stream: "   + Mem. Capacity. tostring ());

Mem. setlength ( 550 );

MessageBox. Show ( " The memory stream length after the setlength method is called: "   + Mem. length. tostring ());

Mem. Capacity =   620 ; // The value cannot be smaller than the Length attribute.
MessageBox. Show ( " Buffer size after the capacity method is called: "   + Mem. Capacity. tostring ());

// Move the read/write pointer to the first 10 bytes from the stream
Mem. Seek ( 10 , Seekorigin. Begin );
MessageBox. Show (MEM. readbyte (). tostring ());
Mem. Close ();
}

The Length attribute of the memory stream represents the actual length of the data stored in it, while the capacity attribute represents the size of the memory space allocated to the memory stream.
You can use byte arrays to create a fixed-size memorystream,Memorystream mem= NewMemorystream (buffer );

In this case, you cannot set the size of the capacity attribute.
You can also create read-only memory stream objects.Memorystream mem= NewMemorystream (buffer, false );

Fliestream is used to access files.
Create a file and write the content: // Create a new file
Filestream fsforwrite =   New Filestream ( " Test. Data " , Filemode. Create );
Try
{
// Write one byte
Fsforwrite. writebyte ( 100 );
Createexampledata ();
// Write a byte array to a file
Fsforwrite. Write (buffer, 0 , Buffer. getlength ( 0 ));
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
//Close file
Fsforwrite. Close ();
}

Open the file and read the content: Private   Void Readfromfile ()
{
Filestream fsforread =   New Filestream ( " Test. Data " , Filemode. Open );
Try
{
// Read one byte
MessageBox. Show ( " The first byte of the file is: " + Fsforread. readbyte (). tostring ());
// Read/write pointer moved to the first 10 bytes
Fsforread. Seek ( 10 , Seekorigin. Begin );
Byte [] BS =   New   Byte [ 10 ];
// Read 10 bytes from the file and put them in the array BS.
Fsforread. Read (BS, 0 , 10 );
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
Fsforread. Close ();}
}

If a program exits but the opened file is not closed, other programs cannot modify or delete the file.

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.