java - Writing to a text file past a particular string -
i'm looking way write text file searching particular string , writing past it. mean want separate text file 2 sections, can write each section separate java programs. reason i'm running tests 2 different java programs , storing results in 2 text files @ moment, have them both write same place, in different sections. here code using write understand may need change in order want do.
try { dateformat dateformat = new simpledateformat("yyyy/mm/dd hh:mm:ss"); calendar cal = calendar.getinstance(); string content = dateformat.format(cal.gettime()) + " test took: " + test3 + " seconds."; file file = new file("mypath\\test.txt"); if (!file.exists()) { file.createnewfile(); } filewriter fw = new filewriter("mypath\\test.txt",true); bufferedwriter bw = new bufferedwriter(fw); fw.write("\r\n"); bw.write(content); fw.write("\r\n"); bw.close(); system.out.println("done"); } catch (ioexception e) { e.printstacktrace(); }
it stores results of each test, separated 1 line. i'm hoping put 2 sections same text file, , able add end of each independently. possible? , if how go doing that?
as long 2 java programs not writing file at same time, 1 way can have 2 programs write different parts of file.
each program create instance of class, , call appropriate methods program (either 1st program or 2nd program).
package com.ggl.testing; import java.io.file; import java.util.arraylist; import java.util.list; public class twopartfile { private string partoneheader; private list<string> partone; private string parttwoheader; private list<string> parttwo; public twopartfile(string partoneheader, string parttwoheader) { this.partoneheader = partoneheader; this.partone = new arraylist<>(); this.parttwoheader = parttwoheader; this.parttwo = new arraylist<>(); } public twopartfile(file file, string partoneheader, string parttwoheader) { readfile(file, partoneheader, parttwoheader); } public void readfile(file file, string partoneheader, string parttwoheader) { // todo write code read file , separate components. } public void writefile(file file) { // todo write code write file components } public list<string> getpartone() { return partone; } public void setpartone(list<string> partone) { this.partone = partone; } public list<string> getparttwo() { return parttwo; } public void setparttwo(list<string> parttwo) { this.parttwo = parttwo; } public string getpartoneheader() { return partoneheader; } public string getparttwoheader() { return parttwoheader; } }
Comments
Post a Comment