Results 1 to 4 of 4
Thread: Reading from and writing to file
- 10-16-2012, 07:09 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 41
- Rep Power
- 0
Reading from and writing to file
Hey guys, so I'm programming a snake game and I'm trying to include a feature that saves a person's high score. I'm trying to use BufferedReader and BufferedWriter to read and write to a .txt file in the bin of my project. (if this isn't the right way to go about it, let me know.) Whenever I initialize the game, the high score is displayed as -1. Apparently this is what is returned when the reader reaches the end of the stream. (not really sure what this means or why its happening.) and even if I pre-enter a number into the .txt file, when I run the program and then go back to look at it, the file is completely empty. I made this SSCCE to show my problem. The score goes up whenever you press a key.
Java Code:import java.applet.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class GameSSCCE extends Applet implements KeyListener{ private int score, highScore; private BufferedReader read; private BufferedWriter save; public void init(){ setSize(200,100); setFocusable(true); addKeyListener(this); try { read = new BufferedReader(new FileReader("High Score Test.txt")); save = new BufferedWriter(new FileWriter("High Score Test.txt")); highScore = read.read();//Reading -1. End of stream? read.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void paint(Graphics g){ g.drawString("High Score: "+highScore, 50, 50); g.drawString("Score: "+score, 50, 65); } public void keyPressed(KeyEvent e) { score++; if(score > highScore) highScore = score; repaint(); } public void stop(){ try { save.write(highScore); save.close(); } catch (IOException e) { e.printStackTrace(); } } public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} }
- 10-16-2012, 11:28 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Reading from and writing to file
Think about how your code works. It first creates a Reader to read from a File. In the very next line, it creates a writer which creates a new File by that name (meaning the previous file is overwritten). In other words, I wouldn't recommend mixing up the reading/writing like this. If you want to read the file, read it and then close it. If you want to write to it, write to it and then close it.
- 10-17-2012, 09:48 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Reading from and writing to file
Yep.
You only need to read from the file once, to get the current highscore.
That should be a method which opens the reader, reads the high score, then closes the reader and returns the value.
You only need to write to the file once, to replace the highscore with a new one.
That also should be a method, which opens the writer, writes the high score, then closes the writer.Please do not ask for code as refusal often offends.
- 10-17-2012, 08:32 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 41
- Rep Power
- 0
Similar Threads
-
Writing and Reading from a file?
By SilentCoder in forum New To JavaReplies: 5Last Post: 06-10-2011, 03:12 AM -
Reading from .xls file and writing to .csv
By Yatta in forum New To JavaReplies: 1Last Post: 04-09-2011, 04:44 PM -
File reading / writing
By MattBSibley in forum New To JavaReplies: 5Last Post: 04-19-2010, 05:20 AM -
Reading and writing to a file
By jigglywiggly in forum New To JavaReplies: 13Last Post: 03-09-2009, 10:44 AM -
Reading/Writing to file
By Doctor Cactus in forum New To JavaReplies: 2Last Post: 10-28-2008, 02:05 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks