FileStream file stream in c #

Source: Internet
Author: User

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}

Related Article

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.