C # Create read-write txt

Source: Internet
Author: User

Try   {        stringFilePath = Application.startuppath +"Tst.txt"; FileStream FS=NewFileStream (FilePath, Filemode.append, FileAccess.Write, Fileshare.none); StreamWriter SW=NewStreamWriter (FS); Sw. WriteLine (""); Sw.        Flush (); Sw.        Close (); Fs.   Close (); }   Catch(IOException ioex) {logger.error ("Write txt failed:"+ioex.tostring ()); }

Filemode.append: The file does not exist, the file is created, the file exists, the stream points to the end of the file, and can only be used in conjunction with enumeration FileAccess.Write .

String FilePath = Application.startuppath + "L.txt"; try{                       FileStream fs = new FileStream (FilePath, FileMode.OpenOrCreate, FileAccess.Write, fileshare.none);       StreamWriter SW = new StreamWriter (fs);       Sw. Basestream.seek (0, seekorigin.end);       Sw. Write ("test");       Sw. Flush ();       Sw. Close ();       Fs. Close ();} catch (IOException Ioex) {       logger.error ("Write txt failed:" + ioex.tostring ());}

FileMode.OpenOrCreate: The file does not exist, the file is created, the file is opened, and the stream points to the beginning of the file. If you append text, you can set seekorigin.end.

stringFilePath = Application.startuppath +"L.txt";Try{FileInfo Finfo=NewFileInfo (FilePath); using(FileStream fs =Finfo. OpenWrite ()) {StreamWriter SW=NewStreamWriter (FS); Sw. Basestream.seek (0, Seekorigin.end); Sw. Write ("Test"); Sw.             Flush (); Sw.             Close (); Fs.        Close (); }}Catch(Ioexcepton ioex) {logger.error ("Write txt failed:"+ioex.tostring ());}

FileInfo provides the openred (), OpenWrite () method to create a FileStream object, opening a read-only, write-only file.
Read txt is used to streareader, there are not many records here:

StreamReader sr = new StreamReader (FilePath);

String Strread;
while ((STRREAD=SR. ReadLine ())!=null)

{

Sbresult.appen (Strread);

}

Sr. Close ();

C # Create read-write txt

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.