Filestream read/write files vs streamwriter and streamreader read/write files

Source: Internet
Author: User

The filestream object indicates the stream that points to the file on the disk or network path. This class provides methods to read and write bytes in files. However, streamreader or streamwriter is often used to perform these functions. This is because the filestream 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.
The simplest constructor for creating a filestream object is as follows:

 

Code
Filestream File= NewFilestream (filename, filemode. member );
Filestream File= NewFilestream (filename, filemode. Member, fileaccess. member );

Fileaccess members:

Members
Description
 
Read
Open a file for read-only
 
Write
Open a file to write only
 
Readwrite
Open a file for reading and writing
 

 

An exception is thrown if the object is not specified by the fileaccess enumeration member. This attribute changes the user's access permissions to files based on the user's authentication level.

In versions where the filestream constructor does not use the fileaccess enumeration parameter, use the default value fileaccess. readwrite.

Filemode enumeration members. What happens when each value is used depends on whether the specified file name represents an existing file.

Members
File stored in
File does not exist
 
Append
Open the file and the stream points to the end of the file. It can only be used together with the enumerated fileaccess. Write
Create a new file. It can only be used with enumeration fileaccess. Write
 
Create
Delete the file and create a new file.
Create a new file
 
Createnew
Throw an exception
Create a new file
 
Open
Open an existing file. The stream points to the beginning of the file.
Throw an exception
 
Openorcreate
Open the file. The stream points to the beginning of the file.
Create a new file
 
Truncate
Open an existing file and clear its content. The stream points to the beginning of the file and retains the initial creation date of the file.
Throw an exception
 

 

The filestream class operates on Byte and byte arrays, while the stream class operates on character data.
Streamwriter allows you to write characters and strings to files. It processes underlying conversions and writes data to filestream objects. Streamreader is similar.
Instance:

Code
///   <Summary>
/// Read/write operations on text files
///   </Summary>
Private   Static   Void Textfileoperate ()
{
// Create and write files
If ( ! File. exists (PATH ))
{
Using (Streamwriter SW = File. createtext (PATH ))
{
Sw. writeline ( " Hello world is writing start " );
Sw. writeline ( " Hello world is writing end " );
}
}
// Read files
If ( ! File. exists (PATH ))
{
Using (Streamreader SD = File. opentext (PATH ))
{
While (SD. Readline () ! =   Null )
{
Console. writeline (SD. Readline ());
}
}
}
// Delete A copy object
File. Copy (path, @" E: \ fileoperatercopy.txt " );
File. Delete (PATH );
}
///   <Summary>
/// Filestream reads files
///   </Summary>
Private   Static   Void Streamfileoperate ()
{
If (File. exists (PATH ))
{
File. Delete (PATH );
}
// Open a file (), or create a file such as: FS = file. Create (path, 1024)
Filestream FS =   New Filestream (path, filemode. createnew );
// Convert to byte write data (Chinese characters can be written)
Byte [] info =   New Utf8encoding ( True ). Getbytes ( " This is some text in the file. " );
FS. Write (info, 0 , Info. Length );
FS. Close ();
// Open a file
FS =   New Filestream (path, filemode. Open, fileaccess. Read );
// Read files
Binaryreader R =   New Binaryreader (FS );
For ( Int I =   0 ; I < Info. length; I ++ )
{
Console. writeline (R. readchar ());
}
FS. Close ();

}

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.