Create a TXT file and write the file content ,...

Source: Internet
Author: User
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 ();}}

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.