Schrodinger error, BufferedWriter does not write to TXT unless manually checked

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

+7
java eclipse slick lwjgl bufferedwriter
source share
3 answers

You need close BufferedWriter in your finally block.

If you wish, you can flush add BufferedWriter to the try block after the write is complete, although the close operation will clear it first.

Here's an example, 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-style ("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(); } 
+4
source share

YAY !!! Having tried everything that you all said, delve deeper into the topic and spend almost 2 days researching the problem. I 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, and one result file that needs to be deleted is deleted. In short, these two files are somehow the same

For 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(); } 

Please note that I did not use src/file.txt just /file.txt , it reads from a file in the bin folder.

For the record 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, note that I did not use src/file.txt , rather, I sent it to a file in the bin folder.

And it worked !!!

In the end, I got ~ 70 lines of code inside the / * ... * / labels, labeled as "// almost working" and at least 10 times more than I already deleted. But finally it works!

Thanks to everyone who commented and answered here, I could not have found a solution without you. Thanks again everyone!

+1
source share

Java makes a distinction between a file and a resource. The resource is on its way to the class, possibly in a bank. The resource should therefore be read-only. It can also be cached.

In your case, using the resource (when reading) is undesirable.

I would do something like the following.

 String userDir = System.getProperty("user.home"); Path txtPath = Paths.get(userDir, "file.txt"); List<String> content = Files.readAllLines(txtPath, StandardCharsets.UTF_8); Files.write(txtPath, content, StandardCharsets.UTF_8); giveToFile = content.get(0); content = Collections.singletonList(giveToFile); 

This saves the file outside the class, so you can create the application in the bank.

You can save the source file read-only file.txt as a resource that functions as the source template and copy it to the user directory.

I used UTF-8 encoding here, so the file format is portable.

0
source share

All Articles