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