FileStream file stream for C #

Source: Internet
Author: User

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)!

  • 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.