File stream
FileStream, StreamReader and StreamWriter can operate large files;
FileStream operation Byte; can manipulate any type of file;
StreamReader and StreamWriter operation characters;
FileStream
Method Name Action Parameter
FileStream () Creates a FileStream object the first is the path, the second is the file mode FileMode enumeration, and the third data pattern fileacess
Read () section reads the file, returns the actual read of the number of valid bytes, if the number of read is not specified by the third parameter, the empty padding is the first is a stored byte array, indicating from where to put an array in the array? , the maximum number of reads per
Write () writes a byte array to the first parameter is a byte array, the second parameter indicates where to start writing, and the third parameter describes the maximum number of writes
Close (), Dispose () closes the stream, freeing the resource occupied by the stream
FileMode OpenOrCreate, Append
Fileacess. Read, Write, Readwirte
The process of creating a file stream object is written in the using, which automatically helps us to release resources;
StreamReader and StreamWriter
The
can be used to read formatted text files;
have ReadLine and write WriteLine methods
<code class= "language-c# HLJS cs" >using system;using system.collections.generic;using System.Diagnostics;using System.io;using system.linq;using system.text;using System.Threading.Tasks; namespace read and write files with FileStream {class Program {static void Main (string[] args) {string str = @ "E:\ download \ Software"; Stopwatch SW = new Stopwatch (); Sw. Start (); using (FileStream fswriter = new FileStream (str + @ "\opencv-3.0.exe", FileMode.Create, FileAccess.Write)) { using (FileStream Fsreader = new FileStream (str + @ "\opencv-2.4.9.exe", FileMode.Open, FileAccess.Read)) {byte[] bytes=new byte[1024*4];//4kb is appropriate; int readnum; while (Readnum=fsreader.read (bytes,0,bytes. Length)!=0)//Less than the description read {fswriter.write (bytes,0,readnum); }}//suing Reader}//using writer Sw. Stop (); Console.WriteLine ("Total run time is {0}", SW. Elapsedmilliseconds); Console.readkey (); }//main}//class}</code>
The above is the content of C # filestream file stream, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!