java - How to write mutilple lines in a text file by calling same method different time? -
i come problem.
this method below:
public void somemethod() { stringbuilder newfile = new stringbuilder(); string edited = "my string line"; newfile.append(edited); newfile.append("\n"); filewriter fstreamwrite = new filewriter("transaction.txt"); bufferedwriter out = new bufferedwriter(fstreamwrite); out.write(newfile.tostring()); out.close(); } and when calling method in main class more 1 time code creating transaction.txt line "my string line". when call method more 1 time write "my string line" several time, overriding line , not giving me output like.
my string line
my string line
my string line
when call method 3 times.
any idea how write same line multiple times calling same method multiple times?
i think want append file . can use constructor filewriter(java.io.file,boolean):
parameters:
file - file object write to
append - if true, bytes written end of file rather beginning
hence change code :
new filewriter("transaction.txt",true); to write new line file , use bufferedwriter#newline().
writes line separator. line separator string defined system property line.separator, , not single newline ('\n') character.
Comments
Post a Comment