FileStream, StreamReader and StreamWriter

Source: Internet
Author: User

FileStream, StreamReader and StreamWriter
The FileStream operation Byte.
StreamReader and StreamWriter manipulate the characters.

Using FileStream to read data
FileStream fsread=new FileStream (@ "C:\new.txt", Filemode.openorcreate,fileaccess.read);
Byte[] Buffer=new byte[1024*1024*5];
3.8M 5M R Returns the number of active sections actually read this time
int R=fsread.read (Buffer,0,buffer. Length);
Decodes each element of a byte array into a string in the specified editing format
String s=encodeing.utf8.getstring (Buffer,0,r);
Close the stream
Fsread.close ();
Releasing resources that are used by the stream
Fsread.dispose ();
Console.WriteLine (s);
Console.readkey ();

//Use FileStream to write data
* The process of creating a file stream object is written in the using, which automatically helps us to release the resources occupied by the stream.
using (FileStream fswrite=new FileStream (@ "C:\new.txt", Filemode.openorcreate,fileaccess.write))
{
String Str= "See if I've got you covered." ";
byte[] Buffer=encoding.utf8.getbytes (str);
Fswrite.write (Buffer,0,buffer. Length);
}
Console.WriteLine ("Write OK");
Console.readkey ();


Using file streams for multimedia file replication
Idea: Read the multimedia file you want to copy before writing it to the location you specified.
string [email protected] "c:\1.wmv";
string [email protected] "c:\2.wmv";
Cotyfile (Source,target);
public static void CopyFile (string source, string target)
{
1 we have a stream that is responsible for reading
using (FileStream fsread=new FileStream (source,filemode.open,fileaccess.read))
{
2 Create a stream that is responsible for writing
using (FileStream fswirte=new FileStream (target,filemode.openorcreate,fileaccess.write))
{
Byte[] Buffer=new byte[1024*1024*5];
while (true)
{
Returns the number of bytes actually read by this read
int R=fsread (Buffer,0,buffer. Length);
If you return a 0, which means that you have not read the data, you are done reading it.
if (r==0) {break;}
Fswrite (BUFFER,0,R);
}

}
}
}

FileStream, StreamReader and StreamWriter

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.