Java – Schrodinger error. Bufferedwriter will not write TXT unless checked manually

I'm a novice programmer - I can't find the answer to this question

I have a problem with this part of the code: (file.txt exists and there is no uppercase letter in the name, givetofile is a string) (no exception is 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();
       }
   }
}

(the same bug is generated)

I put a system at the end of the try block out. Print, it works normally and only runs once I also use g. drawstring, and givetofile always gives the expected string I performed the following two experiments (the program is a game thing. You get the final score according to your performance, put it in the high score, and then rewrite the txt file.) (I suggest reading TLDR before.)

Experiment 1 (file.txt: "0") (successful):

>I ran the program and got 15 points. – String loaded from TXT: "0 0 0 0 0" – givetofile (string): "15 0 0 0" > I double-click the txt file (package browser) on the left side of eclipse, it opens in a new tab, I see in txt: "15 0 0 0", I close the label > I run the program again and get 30 points. – String from text: "15 0 0 0" – givetofile (string): "30 15 0 0" > I double-click the txt file (package browser) on the left side of eclipse, I see in txt: "30 15 0 0", I close the label > I run the program for the last time and get 0 points. – String loaded from TXT: "30 15 0 0 0" – givetofile (string): "30 15 0 0 0"

Experiment 2 (file.txt: "0") (failed):

>I ran the program and got 15 points. – String loaded from TXT: "0 0 0 0" – givetofile (string): "15 0 0" > I double-click the txt file (package browser) on the left side of eclipse, I close the label > I run the program again and get 30 fonts. – String from text: "15 0 0 0" – givetofile (string): "30 15 0 0" > I don't want to double-click the txt file. I don't open it. In the new label, I don't check it. > I ran the program for the last time and got 0 points. – String from TXT: "15 0 0" – givetofile (string): "15 0 0"

Tlrd: the program will not write txt files unless I check manually

There is an error, but no, depending on whether I check the txt file

Sorry, I'm sorry for this problem. I'm sorry if it's super simple, but I'm a beginner and can't find any solution on the Internet. Thank you for your help in advance

Edit:

I use it to close the program: (XPOS and YPOS are mouse coordinates) (basically the original exit button)

if((xpos>= 200 && xpos <= 400) && (ypos>=100 && ypos <=200)){
    if(Mouse.isButtonDown(0)){
        System.exit(0);
    }
}

I got this: (no exception)

Thu APR 30 16:44:14 CEST 2015 Info: slice 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: start displaying 600 × 600 Thu APR 30 16:44:15 CEST 2015 Info: use Java PNG loader = true Thu APR 30 16:44:15 CEST 2015 Info: controller not available

This part reads the file, and no other part does anything to the file. The reader works normally:

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 located in public void init. The type of scoremax is public static final int

Solution

You need to close bufferedwriter. In the finally block

Alternatively, you can refresh the bufferedwriter in the try block after writing, although the close operation will refresh it first

This is an example of reexamination, Java 6 style:

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 styles ("trial" and autoclosables):

try (BufferedWriter bw = new BufferedWriter(new FileWriter("src/file.txt"))) {
    bw.write(giveToFile);
    bw.flush();
}
catch(IOException e){
    e.printStackTrace();
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>