Example 41 file stream read and write

FileStream class

https://docs.microsoft.com/zh-cn/dotnet/api/system.io.filestream?view=netframework-4.8

Namespace: System.IO

Assembly: mscorlib.dll

Provide Stream for files  , supporting both synchronous read and write operations and asynchronous read and write operations.

annotation

Use the  FileStream  class to read, write, open, and close files in the file system. You can also manipulate other operating system handles related to files, including pipes, standard input, and standard output. You can use  Read , Write , CopyTo, and  Flush  methods to perform synchronous operations, or  ReadAsync , WriteAsync , CopyToAsync, and  FlushAsync  methods to perform asynchronous operations. Use asynchronous methods to perform resource-intensive file operations without blocking the main thread. This performance consideration is especially important when a time-consuming stream operation in a Windows 8.x app store application or desktop application may block the UI thread and make your application appear to be not working. FileStream  buffers input and output to improve performance.

 important

This type implements the  IDisposable  interface. After using the type, you should release the type directly or indirectly. To release the type directly , call its  method in the  Disposetry / block  catch. To release types indirectly, use  language constructs using(in C #) or  Using(in Visual Basic). For more information, see   the section "Using objects that implement IDisposable" in the topic of the IDisposable interface.

The IsAsync  property detects whether the file handle has been opened asynchronously. Having  isAsync, useAsyncor  options create a constructor parameter  FileStream  instance class, can specify this value. If  trueattributed, the stream will use overlapping i / o to perform file operations asynchronously. However, it is not necessary   to call the ReadAsync , WriteAsync, or  CopyToAsync  methods with the true IsAsync attribute  . When the  IsAsync  property is called and asynchronous read and write operations are called, the UI thread will still not be blocked, but the actual i / o operations will be performed synchronously. The Seek  method supports random access to files. Seek  allows to move the read / write position to any position in the file. This is achieved by referring to the point parameter by byte offset. The byte offset is relative to the search reference point. It can be the beginning, current position, or end of the base file, and is represented by  the three members enumerated by  SeekOrigin .false 

      When using this class, you need to define an instance of the class, that is, an object, and read and write files by using the properties and methods of this object. The file stream object is defined as follows:
    Dim object As FileStream
    object = NewFileStream (path, mode, access, share, buffersize, isAsync)

    The meaning of each parameter value of the file stream constructor is as follows:

  • path, file path name:
  • Mode, FileMode enumeration class plow, file open mode;
  • Access, the FileAccess enumeration type, determines how the FileStream object accesses the file;
  • Share, FileShare enumeration type, file sharing method;
  • buffersize, the required buffer is small;
  • isAsync, synchronous or asynchronous input and output.

    The constructor of FileStream can be overloaded multiple times. When creating a new file stream object, only the first few parameters can be given.

    The values ​​of the FileMode enumeration type include:

  • Append, add to the end of the file;
  • Create, create a file, you can overwrite;
  • CrcateNew, create a new file;

Open, open the file:

  • OpenOrCreate, open the file, and create it if it doesn't exist.

    The values ​​of the FileAccess enumeration type include:

  • Read, access to the file is to read the file;
  • ReadWrite, read and write files;
  • Write, write files.

    The values ​​of the FileShare enumeration type include:

  • Read, allowing other users to read and access the file;
  • ReadWrite, allowing other users to read and write access to files;
  • Write, allows other users to write to the file.

       After the file stream object is defined, the file content can be read by the Read method, and the file content can be rewritten by the Write method. The Read and Write methods have three parameters. The first parameter is an array, which represents the data read from the file or the array written to the file; the second parameter represents the starting position for reading or writing; This operation should process several data.
    The file is closed using the Close method.

Module Module1

    Sub Main()
        testStream()

        Console.Read()
    End Sub

    Private Sub testStream()
        Dim DataWrite(10) As Byte
        Dim DataRead(10) As Byte
        Dim i As Integer
        For i = 0 To 10
            DataWrite(i) = i * 3
        Next

        Dim testFs As IO.FileStream
        testFs = New IO.FileStream("J:\test\Stream.txt", IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
        testFs.Write(DataWrite, 0, 11)
        testFs.Close()

        testFs = New IO.FileStream("J:\test\Stream.txt", IO.FileMode.Open, IO.FileAccess.Read)
        testFs.Read(DataRead, 0, 11)
        testFs.Close()

        For i = 0 To 10
            Console.WriteLine(DataRead(i))
        Next
    End Sub
End Module
 

Published 146 original articles · won praise 0 · Views 2727

Guess you like

Origin blog.csdn.net/ngbshzhn/article/details/105616070