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