Package COM. h. oa. basic. conn. util; import Java. io. bufferedreader; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. filereader; import Java. io. filewriter; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. io. printwriter; public class writetextservice {public static bufferedreader bufread; // specify the file path and name Private Static string Path = "Q:/sessionid.txt"; Private Static file filename = new file (PATH); Private Static string readstr = ""; public static void creattxtfile () throws ioexception {If (! Filename. exists () {filename. createnewfile (); system. Err. println (filename + "created! ") ;}} Public static string readtxtfile () throws ioexception {string STRs =" "; try {filereader READ = new filereader (filename); stringbuffer sb = new stringbuffer (); char ch [] = new char [1024]; int d = read. read (CH); While (D! =-1) {string STR = new string (CH, 0, d); sb. append (STR); D = read. read (CH);} STRs = sb. tostring ();} catch (filenotfoundexception e) {// todo auto-generated catch blocke. printstacktrace ();} system. out. println ("File Content:" + "\ r \ n" + STRs); Return STRs;} public static void writetxtfile (string newstr) throws
Ioexception {// create the file creattxtfile (); // read the file string STR = readtxtfile (); // write the file filewriter fw = new filewriter (PATH); If (Str. length () <1) {FW. write (newstr);} else {FW. write (STR + "\ r \ n" + newstr);} FW. close ();} public static void replacetxtbystr (string oldstr, string
Replacestr) {string temp = ""; try {file = new file (PATH); fileinputstream FD = new fileinputstream (File); inputstreamreader ISR = new inputstreamreader (FS ); bufferedreader BR = new bufferedreader (ISR); stringbuffer Buf = new stringbuffer (); // Save the content before the row for (Int J = 1; (temp = BR. readline ())! = NULL &&! Temp. Equals (oldstr); j ++)
{Buf = Buf. append (temp); Buf = Buf. append (system. getproperty ("line. separator ");} // insert content into Buf = Buf. append (replacestr); // Save the content after the row while (temp = BR. readline ())! = NULL) {Buf = Buf. append (system. getproperty ("line. separator "); Buf = Buf. append (temp);} BR. close (); fileoutputstream Fos = new fileoutputstream (File); printwriter PW = new printwriter (FOS); PW. write (BUF. tostring (). tochararray (); PW. flush (); PW. close ();} catch (ioexception e) {e. printstacktrace () ;}} public static void main (string [] S) throws ioexception {// create a file creattxtfile (); // read the file readtxtfile (); // write the writetxtfile ("the path is clear but clear"); // read the file readtxtfile ();}}