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):
Experiment 2 (file.txt : "0 0 0 0 0") (failure):
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
You need to close
the BufferedWriter
in your finally
block.
Optionally, you can flush
your BufferedWriter
as well in the try
block after you are finished writing, although the close
operation will flush it first.
Here's a revisited example, Java 6-styled:
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("src/file.txt"));
bw.write(giveToFile);
// bw.flush(); // if needed
}
catch(IOException e){
e.printStackTrace();
}
finally {
if (bw != null) {
try {
bw.close();
}
catch (Throwable t) {
t.printStackTrace();
}
}
}
... and Java 7-styled ("try with" and AutoClosable
s):
try (BufferedWriter bw = new BufferedWriter(new FileWriter("src/file.txt"))) {
bw.write(giveToFile);
bw.flush();
}
catch(IOException e){
e.printStackTrace();
}
YAY!!! After trying out everything you all said, get deeper into the topic and spend almost 2 days researching the problem I got found a solution!!!!
The file is still in the same src
so its path is src/file.txt
but there is a file automatically created in the bin folder with the same name, deleting one results boths file to be deleted. In short, these two files somehow the same
For the code I used this to read the file:
try{
InputStream is = state1.class.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();
}
Notice that I didnt use src/file.txt
just /file.txt
, it reads from the file in the bin folder.
For the write I used:
FileOutputStream fs = null;
try{
fs = new FileOutputStream("bin/file.txt");
OutputStreamWriter ow = new OutputStreamWriter(fs);
BufferedWriter bw = new BufferedWriter(ow);
bw.write(giveToFile);
bw.flush();
bw.close();
}catch (IOException e){
e.printStackTrace();
}finally{
try{fs.close();} catch(IOException e){e.printStackTrace();}
}
And again, notice that I didnt use the src/file.txt
here either, rather I directed it to the file in the bin
folder.
And it worked!!!
At the end I got ~70 row code inside /* ... */ marks labeled as '//almost works' and at least 10times more that I already deleted. But finally it works!!
Thank you for everyone who commented and answered here, I wouldnt be able to find the solutions without you all. Thank you everyone again!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With