I am a rookie programmer-wanna-be and came across this problem I couldn't find the answer for.
I use Eclipse
, and for the program I use slick
and lwjgl-2.9.3
The following code is in a state, inside the public void update(...)
I have the problem with this part of the code:
(the file.txt exists and have no capitals in its name, giveToFile is a String) (no exceptions thrown)
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();
}
}
}
produced the same bug)
I placed a System.out.print at the end of the try block, and it run normally, and run only once. I also used a g.drawString and the giveToFile
always gives the intended String. I executed the following two experiments. (The program is a game-ish thing, you get a score at the end based on your performance and it places it into the high-scores then rewrites the TXT file.) (I suggest to read TLDR before.)
Experiment 1 (file.txt : "0 0 0 0 0") (successful):
- I run the program and earn 15 points.
- string loaded from the txt: "0 0 0 0 0"
- giveToFile (string): "15 0 0 0 0" - I doubleclick the TXT file inside Eclipse at the left side (package explorer), it opens in a new tab and I see inside the txt: "15 0 0 0 0", I close the tab
- I run the program again and earn 30 points.
- string leaded from text: "15 0 0 0 0"
- giveToFile (String): "30 15 0 0 0" - I doubleclick the TXT file inside Eclipse at the left side (package explorer), it opens in a new tab and I see inside the txt: "30 15 0 0 0", I close the tab
- I run the program one last time and earn 0 points.
- string loaded from txt: "30 15 0 0 0"
- giveToFile (string): "30 15 0 0 0"
Experiment 2 (file.txt : "0 0 0 0 0") (failure):
- I run the program and earn 15 points.
- string loaded from the txt: "0 0 0 0 0"
- giveToFile (string): "15 0 0 0 0" - I doubleclick the TXT file inside Eclipse at the left side (package explorer), it opens in a new tab and I see inside the txt: "15 0 0 0 0", I close the tab
- I run the program again and earn 30 ponts.
- string leaded from text: "15 0 0 0 0"
- giveToFile (String): "30 15 0 0 0" - I dont doubleclick the TXT file, I dont open it in a new tab, and I dont check it.
- I run the program one last time and earn 0 points.
- string loeaded from txt: "15 0 0 0 0"
- giveToFile (string): "15 0 0 0 0"
TLRD: the program doesn't write into the TXT file unless I check it manually
there is a bug, and there isn't, depends on if I check the txt file, or not
sorry for the long question and sorry if it is something super simple, but I am a beginner and couldn't 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 mouse coordinates) (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:Starting display 600x600
Thu Apr 30 16:44:15 CEST 2015 INFO:Use 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 the public void init and SCOREMAX's type is public static final int
bw.flush();
just beforebw.close();
– Geomancyclose
invokesflush
. – Checkupnew FileWriter("C:/file.txt")
. And as another, independent test: Try what happens when you select your project root after writing the file, and pressF5
(refresh) (instead of opening the file!), and load it afterwards. – Denialsrc
directory and Eclipse has a "local history". I think that the best solution is to not write into thesrc
directory, anyhow... – Denial