Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schrodinger's bug, BufferedWriter doesn't write into TXT unless manually checked

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):

  1. 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"
  2. 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
  3. 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"
  4. 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
  5. 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):

  1. 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"
  2. 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
  3. 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"
  4. I dont doubleclick the TXT file, I dont open it in a new tab, and I dont check it.
  5. 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

like image 357
stack_overflow_interrogator Avatar asked Apr 30 '15 14:04

stack_overflow_interrogator


2 Answers

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 AutoClosables):

try (BufferedWriter bw = new BufferedWriter(new FileWriter("src/file.txt"))) {
    bw.write(giveToFile);
    bw.flush();
}
catch(IOException e){
    e.printStackTrace();
}
like image 157
Mena Avatar answered Sep 28 '22 22:09

Mena


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!

like image 30
stack_overflow_interrogator Avatar answered Sep 28 '22 23:09

stack_overflow_interrogator