java - Writer not writing anything to file -
public class adddetails extends application { private final string filename = "c:\\users\\marsh\\onedrive\\documents\\customprograms\\calcprob\\players.txt"; private string name; private char hand1, hand1; private double skill1, skill2; file file = new file(filename); fileoutputstream fos = null; writer writer = null; static stage classstage = new stage(); string x = null; public adddetails() { name = ""; hand = '\0'; skill = 0.0; bhand = '\0'; bskill = 0.0; } @suppresswarnings("restriction") @override public void start(stage mystage) throws ioexception { classstage = mystage; mystage.settitle("details"); gridpane rootnode = new gridpane(); rootnode.setpadding(new insets(15)); rootnode.sethgap(5); rootnode.setvgap(5); rootnode.setalignment(pos.center); scene myscene = new scene(rootnode, 300, 200); rootnode.add(new label("name:"), 0, 0); textfield name = new textfield(); rootnode.add(name, 1, 0); rootnode.add(new label("hand:"), 0, 1); textfield hand = new textfield(); rootnode.add(hand, 1, 1); rootnode.add(new label("skill:"), 0, 2); textfield skill = new textfield(); rootnode.add(skill, 1, 2); rootnode.add(new label("skill:"), 0, 3); textfield bskill = new textfield(); rootnode.add(bskill, 1, 3); rootnode.add(new label("hand:"), 0, 4); textfield bhand = new textfield(); rootnode.add(bhand, 1, 4); button sbutton = new button("store"); rootnode.add(sbutton, 1, 5); gridpane.sethalignment(sbutton, hpos.left); button ebutton = new button("finish"); rootnode.add(ebutton, 1, 5); gridpane.sethalignment(ebutton, hpos.right); name.setprompttext("enter name"); hand.setprompttext("enter hand ); skill.setprompttext("enter skill"); bhand.setprompttext("enter hand"); bskill.setprompttext("enter skill"); mystage.setscene(myscene); mystage.show(); try { bufferedreader in = new bufferedreader(new inputstreamreader(new fileinputstream(file), "utf-8")); fos = new fileoutputstream(file, true); writer = new outputstreamwriter(fos, "utf-8"); ebutton.setonaction(e -> { mystage.close(); try { writer.close(); } catch (ioexception writerclose) { alert alert = new alert(alerttype.error); alert.settitle("error"); alert.setheadertext("error encountered"); alert.setcontenttext("error: " + writerclose.getmessage()); } }); sbutton.setonaction(e -> { name = name.gettext(); hand = hand.gettext().charat(0); skill = double.valueof(skill.gettext()); bskill = double.valueof(bskill.gettext()); bhand = bhand.gettext().charat(0); hand = character.touppercase(hand); bhand = character.touppercase(bhand); system.out.println(name + "\t" + hand + "\t" + skill + "\t" + bhand + "\t" + bskill); try { writer.write(name + "\t" + hand + "\t" + skill + "\t" + bhand + "\t" + bskill); name.clear(); hand.clear(); skill.clear(); bskill.clear(); bhand.clear(); } catch (ioexception br) { alert alert = new alert(alerttype.error); alert.settitle("error"); alert.setheadertext("error encountered"); alert.setcontenttext("error: " + br.getmessage()); } }); } catch (ioexception e) { alert alert = new alert(alerttype.error); alert.settitle("error"); alert.setheadertext("error encountered"); alert.setcontenttext("error: " + e.getmessage()); } { try { if (writer != null) writer.close(); if (fos != null) fos.close(); } catch (ioexception finalclose) { alert alert = new alert(alerttype.error); alert.settitle("error"); alert.setheadertext("error encountered"); alert.setcontenttext("error: " + finalclose.getmessage()); } } } public static void main(string[] args) { launch(args); } }
you're closing output file after it's opened, without writing anything.
the open , close run in main thread when start
called, while attempted write happens in event thread when sbutton
clicked.
a simple fix open , close fos
in sbutton
action listener doing write.
Comments
Post a Comment