I am a novice programmer - I want and have encountered this problem, to which I could not find an answer.
I use Eclipse , and for the program I use slick and lwjgl-2.9.3
The following code is in state, inside public void update (...)
I have a problem with this piece of code:
(file.txt exists and does not have capitals in its name, giveToFile is a string) (there were no exceptions)
try{ BufferedWriter bw = new BufferedWriter(new FileWriter("src/file.txt")); bw.write(giveToFile); bw.close(); }catch(IOException e){ e.printStackTrace(); }
(EDIT:
try{ bw = new BufferedWriter(new FileWriter("src/file.txt")); bw.write(giveToFile); bw.flush(); }catch(IOException e){ e.printStackTrace(); }finally { if (bw != null){ try { bw.close(); }catch (Throwable t){ t.printStackTrace(); } } }
made the same error)
I put System.out.print at the end of the try block, and it starts normally and only starts once. I also used g.drawString, and giveToFile always indicates the intended string. I performed the following two experiments. (The program is an ish game, you get the score at the end based on your performance, and it puts it in the top marks, and then overwrites the TXT file.) (I suggest reading TL; DR earlier.)
Experiment 1 (.txt file: "0 0 0 0 0") (successful):
- I run the program and earn 15 points.
- line loaded from txt: "0 0 0 0 0"
- giveToFile (string): "15 0 0 0 0" - I double-clicked the TXT file inside Eclipse on the left side (explorer), it opens in a new tab, and I see inside txt: "15 0 0 0 0", I close the tab
- I run the program again and earn 30 points.
- string output from the text: "15 0 0 0 0"
- giveToFile (String): "30 15 0 0 0" - I double-clicked the TXT file inside Eclipse on the left side (explorer), it opens in a new tab, and I see inside txt: "30 15 0 0 0", I close the tab
- I run the program for the last time and earn 0 points.
- line loaded from txt: "30 15 0 0 0"
- giveToFile (string): "30 15 0 0 0"
Experiment 2 (.txt file: "0 0 0 0 0") (failed):
- I run the program and earn 15 points.
- line loaded from txt: "0 0 0 0 0"
- giveToFile (string): "15 0 0 0 0" - I double-clicked the TXT file inside Eclipse on the left side (explorer), it opens in a new tab, and I see inside txt: "15 0 0 0 0", I close the tab
- I run the program again and earn 30 show-offs.
- string output from the text: "15 0 0 0 0"
- giveToFile (String): "30 15 0 0 0" - I am not duplicating a TXT file, I am not opening it in a new tab, and I am not checking it.
- I run the program for the last time and earn 0 points.
- line lost from txt: "15 0 0 0 0"
- giveToFile (string): "15 0 0 0 0"
TLRD: the program does not write to the TXT file unless I manually check it
there is an error, and it doesnβt exist, it depends on whether I check the txt file or not
Sorry for the long question and sorry if this is something super simple, but I'm new and could not find any solution on the Internet, thanks for the help in advance
EDIT:
I use this to close the program: (xpos and ypos are the coordinates of the mouse) (basically a primitive exit button)
if((xpos>= 200 && xpos <= 400) && (ypos>=100 && ypos <=200)){ if(Mouse.isButtonDown(0)){ System.exit(0); } }
I got this: (no exceptions)
Thu Apr 30 16:44:14 CEST 2015 INFO: Slick Build # 237
Thu Apr 30 16:44:14 CEST 2015 INFO: LWJGL Version: 2.9.3
Thu Apr 30 16:44:14 CEST 2015 INFO: OriginalDisplayMode: 1366 x 768 x 32 @ 60Hz
Thu Apr 30 16:44:14 CEST 2015 INFO: TargetDisplayMode: 600 x 600 x 0 @ 0Hz
Thu Apr 30 16:44:15 CEST 2015 INFO: launch of the display 600x600
Thu Apr 30 16:44:15 CEST 2015 INFO: using Java PNG Loader = true
Thu Apr 30 16:44:15 CEST 2015 INFO: Controllers not available
This part reads the file, no other part does anything with the file, and the reader works fine:
try{ InputStream is = getClass().getResourceAsStream("/file.txt"); Scanner fileIn = new Scanner(is); for(int i=0; i<SCOREMAX; i++){ scoreInt[i] = fileIn.nextInt(); } fileIn.close(); }catch (Exception e) { e.printStackTrace(); }
it is inside public void init and type SCOREMAX public static final int