java - PrintWriter wont write to .txt file -
i have tried reviewing old threads topic none of solutions seem work me. have trying write .txt file named "receipt.txt" following code getting nothing. no file created (and nothing in file doesn't exist) no errors nothing.
//write file string filename = "receipt.txt"; try{ printwriter writer = new printwriter(filename); writer.println("thank shopping us"); writer.println("name: " + customername); writer.println("returning customer: " + previouscustomer ); writer.println("phone: " + phonenumber); writer.println("shirt type: " + shirttype); writer.println("shirt color: " + shirtcolor); writer.println("shirt material: " + shirtmaterial); writer.println("item amount: " + itmamt); writer.println("total cost: " + fmt3.format(totalcostmsg)); writer.close(); writer.flush(); system.out.println("receipt printed"); } catch (filenotfoundexception e) { e.printstacktrace(); }
your output writen file named filename
replace
printwriter writer = new printwriter("filename");
with
printwriter writer = new printwriter(filename);
edit:
apparenttly not looking file in wrong place, add following statement see has bee created:
system.out.println(new file(filename).getabsolutepath());
also note order of close
, flush
not correct. flush
should called first.
Comments
Post a Comment