The difference and usage of File,fileinfo,filestream,streamreader

Source: Internet
Author: User

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

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.