Tag: write string except defragment long ring write ATI OID
Collated the information on the Internet, data appended to the TXT file three ways, see the following code:
Method One:
1 Public voidmethod1 () {2FileWriter FW =NULL;3 Try {4 //Append Content If the file exists, or create file if the file does not exist5File f=NewFile ("E:\\dd.txt");6FW =NewFileWriter (F,true);7}Catch(IOException e) {8 e.printstacktrace ();9 }TenPrintWriter PW =NewPrintWriter (FW); OnePw.println ("Append content"); A Pw.flush (); - Try { - Fw.flush (); the pw.close (); - fw.close (); -}Catch(IOException e) { - e.printstacktrace (); + } -}
Method Two:
1 Public Static voidmethod2 (string file, String conent) {2BufferedWriter out =NULL;3 Try {4out =NewBufferedWriter (NewOutputStreamWriter (5 NewFileOutputStream (file,true)));6Out.write (conent+ "\ r \ n");7}Catch(Exception e) {8 e.printstacktrace ();9}finally {Ten Try { One out.close (); A}Catch(IOException e) { - e.printstacktrace (); - } the } -}
Method Three:
1 Public Static voidmethod3 (String fileName, string content) {2 Try {3 //open a random Access file stream, read and write4Randomaccessfile Randomfile =NewRandomaccessfile (FileName, "RW");5 //file length, number of bytes6 LongFilelength =randomfile.length ();7 //moves the write file pointer to the end of the file. 8 Randomfile.seek (filelength);9Randomfile.writebytes (content+ "\ r \ n");Ten randomfile.close (); One}Catch(IOException e) { A e.printstacktrace (); - } - } the}
Java Append write TXT file