Filestream operation byte and byte array

Source: Internet
Author: User
Code
The fielstream class operates on Byte and byte arrays, while the stream class operates on character data. Character data is easy to use, but some operations, such as random File Access (access to data at a certain point in the file), must be executed by the filestream object.
Fielstream class maintains the internal file pointer, which points to the position of the next read/write operation in the file.
Filestream can only process raw bytes (raw Byte ). The filestream class can be used to process any data file.
By reading byte data, filestream can be used to read images and sounds. The cost is that the filestream class cannot directly read data into strings.
This pointer can be modified using the seek method. It has two parameters:
The first parameter specifies the moving distance of the file pointer in bytes.
The second parameter specifies the start position of the calculation.

byte [] bydatavalue = New byte [ 200 ];
char [] chardatavalue = New char [ 200 ];

///   <Summary>
/// Filesstream reading usage
///   </Summary>
Private   Void Filesstreamreadfile ()
{
Try
{
Filestream fsfile =   New Filestream ( @" D: \ log. CS " , Filemode. Open );
// The file pointer is moved to the 135 bytes of the file.
Fsfile. Seek ( 135 , Seekorigin. Begin );
// Read the following bytes to array.
Fsfile. Read (bydatavalue, 0 , 200 );
}
Catch (Exception E)
{
Throw E;
}
// Convert bytes into characters
Decoder DC = Encoding. utf8.getdecoder ();
// Byte array to character array for easy display
DC. getchars (bydatavalue, 0 , Bydatavalue. length, chardatavalue, 0 );

}

///   <Summary>
/// Filesstream write usage
///   </Summary>
Private   Void Filestreamwritefile ()
{
Filestream fsfile =   New Filestream ( @" D: \ log.txt " , Filemode. Create );
Chardatavalue = " This is test string " . Tochararray ();
Bydatavalue = New   Byte [Chardatavalue. Length];

//Converts a character array to a byte array.
Encoder EC=Encoding. utf8.getencoder ();
EC. getbytes (chardatavalue,0, Chardatavalue. length, bydatavalue,0,True);

// Set the starting position of the pointer
Fsfile. Seek ( 0 , Seekorigin. Begin );
// Write files
Fsfile. Write (bydatavalue, 0 , Bydatavalue. Length );
}
Filestream objects are usually used in streamwriter or streamreader and are used to process files.
///   <Summary>
/// Streamwriter write Method
///   </Summary>
Private   Void Streamwritermetod ()
{
Try
{
Filestream fsfile =   New Filestream ( @" D: \ log.txt " , Filemode. openorcreate );
Streamwriter swwriter =   New Streamwriter (fsfile );
// Write Data
Swwriter. writeline ( " Hello wrold. " );
Swwriter. writeline ( " It is now {0} " , Datetime. Now. tolongdatestring ());
Swwriter. Close ();
}
Catch (Exception E)
{

ThrowE;
}
}

///   <Summary>
/// Streamwriter reading Method
///   </Summary>
Private   Void Streamreadermetod ()
{
String Sline =   "" ;
Try
{
Filestream fsfile =   New Filestream ( @" D: \ log.txt " , Filemode. Open );
Streamreader srreader =   New Streamreader (fsfile );
// Read files (this method is not recommended when reading large files)
Sline = Srreader. readtoend ();
Txbvalue. Text = Sline;
Srreader. Close ();
}
Catch (Exception E)
{
Throw E;
}
}
///   <Summary>
/// Streamwriter read method 2
///   </Summary>
Private   Void Streamreadermetodtwo ()
{
Try
{
Filestream fsfile =   New Filestream ( @" D: \ log.txt " , Filemode. Open );


Streamreader srreader=   New Streamreader (fsfile );
Int Ichar;
Ichar = Srreader. Read ();
While (Ichar ! =   - 1 )
{
Txbvalue. Text + = (Convert. tochar (ichar ));
Ichar = Srreader. Read ();
}
Srreader. Close ();
}
Catch (Exception E)
{
Throw E;
}
}
Note: When a file exists or does not exist, the filemode enumerated Member values

 
Append
When a file exists: When the file is opened, the stream points to the end of the file and can only be used together with the enumerated fileacess. Write
When the file does not exist: Create a new file. It can only be used with enumeration fileacess. Write
 
Create
When the file exists: delete the file and create a new file.
When the file does not exist: Create a new file
 
Createnew
When the file exists: An exception is thrown.
When the file does not exist: Create a new file
 
Open
When the file exists: Open the existing file, and the stream points to the beginning of the file
If the file does not exist: An exception is thrown.
 
Openorcreate
When a file exists: open the file, and the stream points to the beginning of the file
When the file does not exist: Create a new file
 
Truhncate
When the file exists: Open the existing file and clear its content. The stream points to the beginning of the file and retains the initial creation date of the file.
If the file does not exist: An exception is thrown.

This article is from the csdn blog. For more information, see http://Blog.csdn.net/zhangguoliang0210/archive/2009/04/30/4138428.aspx

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.