File FileStream StreamWriter StreamReader Read and write operation method

Source: Internet
Author: User

stringPath ="D:\\accountchecking\\test.txt";stringContent ="Abcdefg\r\nhigklmn\r\nopqrst";//Manipulate folder objects, none Create folders, you can create multiple levels at onceDirectory.CreateDirectory ("d:\\accountchecking\\");#regionfile-typical operations for files, such as copying, moving, renaming, creating, opening, deleting, and appending classes to a single file at a time//Write a filefile.writealltext (path, content, Encoding.default); File.writealllines (path, content. Split ('R'), Encoding.default); File.writeallbytes (Path, Encoding.Default.GetBytes (content));//Append Contentfile.appendalltext (path, content, Encoding.default);//Read the filestringcontent_t =file.readalltext (path, encoding.default);string[] content_s =file.readalllines (path, encoding.default);byte[] Content_b =file.readallbytes (path);#endregion#regionFilestream-reads, writes, opens and closes files on the file system as a stream, supporting both synchronous read and write operations and asynchronous read and write operations//Write a fileusing(FileStream FileStream =NewFileStream (path, FileMode.Create)) {    byte[] data =Encoding.Default.GetBytes (content); FileStream.Write (data,0, data. Length);//write content to buffer//Flush () writes the buffer contents to the file system, which is automatically written to the file system when it is closed or out of use .//Filestream.flush (); //using a closed stream that does not have to be displayed, it automatically shuts down and releases//filestream.close ();}//Append Contentusing(FileStream FileStream =NewFileStream (path, filemode.append)) {    byte[] data =Encoding.Default.GetBytes (content); FileStream.Write (data,0, data. Length);}//Read the fileusing(FileStream FileStream =NewFileStream (path, FileMode.Open)) {    byte[] data =New byte[Filestream.length]; FileStream.Read (data,0, data.    Length); stringresult =Encoding.Default.GetString (data); //Do something ...}#endregion#regionStreamwriter&streamreader-streamreader implements the abstract base class TextReader class, and StreamWriter implements the abstract base class TextWriter, respectively, for the reading and writing of the stream.//Write a fileusing(StreamWriter StreamWriter =NewStreamWriter (path)) {    //Method Onestreamwriter.write (content); StreamWriter.Flush ();//Write buffer contents to disk//using a closed stream that does not have to be displayed, it automatically shuts down and releases//StreamWriter.Close (); //Method TwoStreamwriter.writeline (content. ToCharArray ());//Write by line//StreamWriter.Flush ();}//Append Contentusing(StreamWriter StreamWriter =NewStreamWriter (Path,true)){    //Method Onestreamwriter.write (content);    StreamWriter.Flush (); //Method TwoStreamwriter.writeline (content. ToCharArray ());//Append by Row}//read a file (StreamReader like pointer displacement, so instantiate multiple objects)using(StreamReader streamreader_toline =NewStreamReader (path))using(StreamReader streamreader_toend =NewStreamReader (path))using(StreamReader streamreader_to =NewStreamReader (path)) {    stringmsg =String.Empty; //Method One     while(!Streamreader_toline.endofstream) {msg=Streamreader_toline.readline (); //Do something ...    }    //Method Two    if(!Streamreader_toend.endofstream) {msg=Streamreader_toend.readtoend (); //Do something ...    }    //Method Three    Char[] data =New Char[StreamReader_To.BaseStream.Length]; Streamreader_to.read (data,0, data.    Length); stringresult =NewString (data); //Do something ...}#endregion

File FileStream StreamWriter StreamReader Read and write operation method

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.