This chapter describes: the basic functions of the FileStream class and simple examples;
1. Reference namespace:using ;
2. Note:When using IO to operate files, be careful about stream closing and release issues!
Strong recommendation: Writing the process of creating file flow objects in using will automatically help us release resources;
Use try{} catch(Exception ex){} to perform a capture;
3. FileStream operates bytes and can operate any type of file;Here is a brief introduction to the methods and parameters of the FileStream class:
(1) FileStream() Function: Create a FileStream object, parameters: the first is the path, the second is the file mode FileMode enumeration, and the third is the data mode FileAcess
FileStream(String, FileMode): FileStream(String, FileMode, FileAccess) FileStream(String, FileMode, FileAccess, FileShare) FileStream(String, FileMode, FileAccess, FileShare, Int32)
Using a constructor containing file share attributes() when initializing FileStream is safer and more efficient than using custom thread locks
(2) FileMode (how to open or create a file): CreateNew (create new file), Create (create and overwrite), Open (open), OpenOrCreate (open and create), Truncate (overwrite file), Append (append);
(3) FileAcess (how the file stream object accesses the file): Read (read only), Write (write), ReadWirte (read and write);
(4) FileShare (how the process shares files): None (reject sharing), Read, Write, ReadWrite (read and write at the same time), Delete;
(5) bufferSize (buffer size setting)
4、(array<Byte[], Int32, Int32):Read a byte from the stream and write the data to the given buffer;
5、(array<Byte[], Int32, Int32):Write a byte block to this stream using data in the buffer;
6、close():Close the current stream and release any resources (such as sockets and file handles) associated with the current stream;
7、dispose():Free all resources used by the stream;
8、CopyTo(Stream):Read all bytes from the current stream and write them to the target stream.
CopyTo(Stream, Int32): Read all bytes from the current stream and write them to the target stream using the specified buffer size
9. Seek() (FileStream class maintains an internal file pointer, which points to the location in the file where the next read and write operation is performed): Set the current position of this stream to the given value. ((Int64, SeekOrigin)
The first parameter specifies the distance the file pointer moves in bytes. The second parameter specifies the starting position of the calculation; the SeekOrigin enumeration contains 3 values: Begin, Current, and End;
For example: (0, );
10. Since the file sharing mode is set to allow subsequent writing, even if multiple threads write files at the same time, they will wait for the previous thread to finish writing before executing, without an error
11. Simple example 1:Simple file writing
FileStream devStream = new FileStream(devPath, , , ,512); (data, 0, 128); if(devStream != null) ();
12. Simple example 2:Write to the file in append mode
public static class MonitData { public static string devPath = ; private static object objLock = new object(); public static void WriteInfo(byte[] data) { lock (objLock) { if (!(devPath)) { byte[] byteArray = new byte[128]; (data, 0, byteArray, 0, 128); if (byteArray != null && == 128) { using ( fs = (devPath)) { (0, ); (byteArray, 0, ); (); (); } } } } } }
13. Simple example:File stream writing
public static void Main(string[] args) { String str = @"E:\Download\Software"; Stopwatch sw = new Stopwatch(); (); using (FileStream fsWriter = new FileStream(str + @"\opencv-3.", , )) { using (FileStream fsReader = new FileStream(str + @"\opencv-2.4.", , )) { byte[] bytes=new byte[1024*4];//4kB is suitable; int readNum; while((readNum=(bytes,0,))!=0)//The explanation is finished { (bytes,0,readNum); fsWriter .Flush();//Clear the buffer and write all data into the file (); (); } } } (); ("The total running time is{0}",); (); }
14. Simple example:Read the file
public static string FileStreamReadFile(string filePath) { byte[] data = new byte[100]; char[] charData = new char[100]; FileStream file = new FileStream(filePath, ); //The file pointer points to position 0 (0, );//The first parameter can be set //Read in two hundred bytes (data, 0, (int) ); //Extract byte array Decoder dec = Encoding.(); (data, 0, , charData, 0); (); (); return (charData); }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.