FileStream file stream in c #
File stream
FileStream, StreamReader, and StreamWriter can operate large files;
FileStream operation byte; can operate on any type of files;
StreamReader and StreamWriter operator characters;
FileStream
Method Name |
Function |
Parameters |
FileStream () |
Create a FileStream object |
The first is the path, the second is the file mode FIleMode enumeration, and the third is the data mode FileAcess. |
Read () |
Partial File Reading, returns the actual number of valid bytes read. If the number of bytes read is not specified by the third parameter, It is null. |
The first is the stored byte array, which indicates where to put the array in the array ?, Maximum number of reads per time |
Write () |
Write a byte array |
The first parameter is a byte array, the second parameter indicates where the data is written, and the third parameter indicates the maximum number of data writes. |
Close (), dispose () |
Close the stream and release the resources occupied by the stream. |
|
FileMode OpenOrCreate, Append
FileAcess. Read, Write, ReadWirte
Writing the process of creating a file stream object in using will automatically help us release resources;
StreamReader and StreamWriter
It can be used to read and format text files;
There are ReadLine and Write WriteLine Methods
Using System; using System. collections. generic; using System. diagnostics; using System. IO; using System. linq; using System. text; using System. threading. tasks; Use FileStream to read and write files in namespace {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. writ E) {using (FileStream fsReader = new FileStream (str + @ "\ opencv-2.4.9.exe", FileMode. open, FileAccess. read) {byte [] bytes = new byte [1024*4]; // 4 kb is suitable; int readNum; while (readNum = fsReader. read (bytes, 0, bytes. length ))! = 0) // indicates that the read is complete {fsWriter. write (bytes, 0, readNum) ;}// suing reader} // using writer sw. stop (); Console. writeLine ("total running time: {0}", sw. elapsedMilliseconds); Console. readKey () ;}// main} // class}