Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By doWhile

Thread: Reading from and writing to file

  1. #1
    dyelax is offline Member
    Join Date
    Oct 2010
    Posts
    41
    Rep Power
    0

    Default 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) {}
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default 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.
    Tolls likes this.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default 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.

    ** This space for rent **

  4. #4
    dyelax is offline Member
    Join Date
    Oct 2010
    Posts
    41
    Rep Power
    0

    Default Re: Reading from and writing to file

    This solved the problem, thanks!

Similar Threads

  1. Writing and Reading from a file?
    By SilentCoder in forum New To Java
    Replies: 5
    Last Post: 06-10-2011, 03:12 AM
  2. Reading from .xls file and writing to .csv
    By Yatta in forum New To Java
    Replies: 1
    Last Post: 04-09-2011, 04:44 PM
  3. File reading / writing
    By MattBSibley in forum New To Java
    Replies: 5
    Last Post: 04-19-2010, 05:20 AM
  4. Reading and writing to a file
    By jigglywiggly in forum New To Java
    Replies: 13
    Last Post: 03-09-2009, 10:44 AM
  5. Reading/Writing to file
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-28-2008, 02:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •