Home  >  Article  >  Backend Development  >  c# FileStream file stream

c# FileStream file stream

黄舟
黄舟Original
2016-12-27 14:03:372611browse

File stream

FileStream, StreamReader and StreamWriter can operate large files;
FileStream operates bytes; can operate any type of file;
StreamReader and StreamWriter operate characters;

FileStream

##Method name                                                                                                                                                                                                                                                        Function For example, the third data mode FileAcess

Read() reads the file in parts and returns the number of valid bytes actually read. If the number read is not specified by the third parameter, fill it with nulls. One is the stored byte array, indicating where to put the array into the array? , the maximum number of reads each time

Write() Writes the byte array The first parameter is the byte array, the second parameter indicates where to start writing, and the third parameter indicates the maximum number of writes

close(), dispose() Close the stream and release the resources occupied by the stream

FileMode OpenOrCreate, Append

FileAcess. Read, Write, ReadWirte

will create The process of file stream object is written in using, which will automatically help us release resources;

StreamReader and StreamWriter

can be used to read formatted text files;

There are ReadLine and Write WriteLine Method

<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 用FileStream读写文件
{
    class Program
    {
        static void Main(string[] args)
        {
            String str = @"E:\下载\软件";
            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是合适的;
                    int readNum;
                    while((readNum=fsReader.Read(bytes,0,bytes.Length))!=0)//小于说明读完了
                    {
                        fsWriter.Write(bytes,0,readNum);
                    }
 
 
                }//suing reader
            }//using writer
            sw.Stop();
            Console.WriteLine("总的运行时间为{0}",sw.ElapsedMilliseconds);
            Console.ReadKey();
 
        }//main
    }//class
}
</code>

The above is the content of c#'s FileStream file stream. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn