In summary, File,fileinfo,filestream is the class used for file I/O, and StreamReader is the class used to read from and write to the stream, using System.IO before use.
Define a TXT document path First: string Txtpath = (@ "d:\c# exercise \1.txt"); To read into this document.
(1) File provides static methods for creating, copying, deleting, moving, and opening files, and assists in creating FileStream.
FileStream fs = File.Open (Txtpath, FileMode.Open);
File can directly invoke various methods (Open, Delete, exists, etc.)
For example: if (file.exists (Txtpath)) {file.delete (Txtpath);}
(2) FileInfo provides instance methods for creating, copying, deleting, moving, and opening files, and assists in creating FileStream.
FileInfo fi = new FileInfo (Txtpath); Instantiation of
FileStream fs = fi. Open ();
(3) FileStream supports random access to files through its Seek method. By default, FileStream opens the text in a synchronous way
, but it also supports asynchronous operations.
With FileStream we can get the streams of a file and then read it.
(4) StreamReader reads characters from Streams by using Encoding to convert characters and bytes.
StreamWriter writes characters to Streams by using Encoding to convert characters to bytes.
StreamReader sr = new StreamReader (FS);
string str = NULL; String Temp=null; while ((TEMP=SR. ReadLine ())!=null) {str+= "" +temp;}
Gets a string that can then be processed for the string.
Ps:
TextReader is the abstract base class for StreamReader and StringReader. The implementation of the abstract Stream class is used for byte input and output, while the TextReader implementation is used for Unicode character output.
TextWriter is the abstract base class for StreamWriter and StringWriter. The implementation of the abstract Stream class is used for byte input and output, while the TextWriter implementation is used for Unicode character output.
The method of the file class verifies the security mechanism every time, so the file efficiency is high when used in a small amount of time, but if it is used more fileinfo efficient
The difference and usage of File,fileinfo,filestream,streamreader