Serializable

New Territory for Me

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1315 Views - Last Post: 11 June 2010 - 04:22 AM Rate Topic: -----

#1 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Serializable

Posted 08 June 2010 - 04:34 PM

So I am trying to make a high score feature on my game where after each game, the high score is stored and then reloaded the next time around. I know I have to use Object Serialization and whatnot, but I'm stepping into uncharted territory. I have some code and know how it all should work, though I'm afraid what should happen and what does happen are two completely separate things.

I read a couple tutorials online, including ones here, but I must have gotten something wrong.


import java.io.Serializable;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import java.io.IOException;

public class HighScore implements Serializable
{
	private Integer highScore;
	
	public HighScore()
	{
		highScore = 0;
	}
	
	public int getHighScore() { return highScore; }
	public void setHighScore(int i) { highScore = i; }
	
	public void writeObject() throws IOException
	{
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("score.out"));
		oos.writeObject(highScore);
		oos.flush();
		oos.close();
	}
	
	public Integer readObject() throws IOException, ClassNotFoundException
	{
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("score.out"));
		return (Integer) ois.readObject();
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Serializable

#2 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Serializable

Posted 08 June 2010 - 04:46 PM

Whats the problem thats occuring? Could you explain in detail.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Serializable

Posted 08 June 2010 - 04:52 PM

You do not want to write the Integer highScore contained in your class HighScore as you do

               oos.writeObject(highScore); 


you want to write your Object HighScode
               oos.writeObject(this); 



Samething for your read it should reaturn a HighScore object

If not, no need for HighScore to be serializable

Don't need highScrore to be an Integer it can be an int

And change your variable names: a highScore in a Class HighScore is really confusing
Was This Post Helpful? 3
  • +
  • -

#4 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Serializable

Posted 08 June 2010 - 05:14 PM

Well my problem for one is that no file (that I can tell) is being created. I was under the impression that after I create the file, it would pop up in my folder or something.

Also, it's simply not loading the file. Should this code work?


package gameFiles.util;

import java.io.Serializable;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import java.io.IOException;

public class HighScore implements Serializable
{
	private int score;
	
	public HighScore()
	{
		score = 0;
	}
	
	public int getHighScore() { return score; }
	public void setHighScore(int i) { score = i; }
	
	public void writeObject() throws IOException
	{
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("score.out"));
		oos.writeObject(this);
		oos.flush();
		oos.close();
	}
	
	public HighScore readObject() throws IOException, ClassNotFoundException
	{
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("score.out"));
		return (HighScore) ois.readObject();
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Serializable

Posted 08 June 2010 - 05:21 PM

Your flush() is useless in the writeObject()
You should probably make you readObject() method static as you may want to access it without passing by another already created HighScore object you can also pass the file name as parameter
You should close your ObjectInputStream in your readObject method before exiting it
        static public HighScore readObject(String filename) throws IOException, ClassNotFoundException 
        { 
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename)); 
                HighScore obj = (HighScore) ois.readObject(); 
                ois.close(); 
                return obj; 
        } 



beside that, and even on its actual state, your code should work

This post has been edited by pbl: 08 June 2010 - 05:37 PM
Reason for edit:: Add the static to readObject() and its filename parameter

Was This Post Helpful? 2
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,127
  • Joined: 27-December 08

Re: Serializable

Posted 08 June 2010 - 06:07 PM

@zim1985: Just my two-cents, but Serialization is better suited for Objects like Players, which have a ton of information and unique characteristics associated with them, not High Scores, which have maybe one or two characteristics. This makes it a lot easier to recover all this information rather than have to parse through the File to create the Object. In the context of what you are trying to do, it might be easier and more efficient just to write to an XML or standard text file than to worry about Serialization for a single score.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Serializable

Posted 08 June 2010 - 07:42 PM

View Postmacosxnerd101, on 08 June 2010 - 07:07 PM, said:

@zim1985: Just my two-cents, but Serialization is better suited for Objects like Players, which have a ton of information and unique characteristics associated with them, not High Scores, which have maybe one or two characteristics. This makes it a lot easier to recover all this information rather than have to parse through the File to create the Object. In the context of what you are trying to do, it might be easier and more efficient just to write to an XML or standard text file than to worry about Serialization for a single score.

I guess we are just making a proof of concept here... which is fine
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,127
  • Joined: 27-December 08

Re: Serializable

Posted 08 June 2010 - 07:46 PM

Agreed. Just thought I'd include some applications as well.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Serializable

Posted 08 June 2010 - 07:49 PM

View Postmacosxnerd101, on 08 June 2010 - 08:46 PM, said:

Agreed. Just thought I'd include some applications as well.

:^:
Was This Post Helpful? 0
  • +
  • -

#10 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Serializable

Posted 08 June 2010 - 08:42 PM

I was at my sister's band concert and I was thinking likewise. I guess I just really wanted to use Serialization :P

Maybe if I implement saving states of the entire game I could do that, but not right now.

So I need to make a FileOutputStream that would write a new file and use a Scanner to read it?
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Serializable

Posted 08 June 2010 - 08:53 PM

View Postzim1985, on 08 June 2010 - 09:42 PM, said:

I was at my sister's band concert and I was thinking likewise. I guess I just really wanted to use Serialization :P

Maybe if I implement saving states of the entire game I could do that, but not right now.

So I need to make a FileOutputStream that would write a new file and use a Scanner to read it?

Depends on how lazy you are.
I would rather save my whole game (with ArrayList and all classes) then little chunk in little files
Was This Post Helpful? 0
  • +
  • -

#12 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Serializable

Posted 08 June 2010 - 11:02 PM

I tried implementing the file technique instead and I got it working...kinda. My only issue is that the numbers aren't saving and loading correctly.


package gameFiles.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;

public class HighScore
{
	private int score;
	private File dataFile;
	
	public HighScore()
	{
		try
		{
			dataFile = new File("highScoreData.txt");
			FileInputStream fis = new FileInputStream(dataFile);
			score = fis.read();
			fis.close();
		}
		catch(FileNotFoundException fnfe)
		{
			System.out.println("File not found!");
			System.out.println("The error was:" + fnfe.getClass().getName()+" "+fnfe.getMessage());
		}
		catch(IOException ioe)
		{
			System.out.println("Error!");
			System.out.println("The error was:" + ioe.getClass().getName()+" "+ioe.getMessage());
		}
	}
	
	public int getHighScore() { return score; }
	
	public void writeFile(int scoreInput)
	{
		try
		{
			FileOutputStream fos = new FileOutputStream(dataFile);
			fos.write(scoreInput);
			fos.close();
		}
		catch(FileNotFoundException fnfe)
		{
			System.out.println("File not found!");
			System.out.println("The error was:" + fnfe.getClass().getName()+" "+fnfe.getMessage());
		}
		catch(IOException ioe)
		{
			System.out.println("Error!");
			System.out.println("The error was:" + ioe.getClass().getName()+" "+ioe.getMessage());
		}
		
	}
	
	
}



Suffice it to say that I am only passing in ints in the main class I am using this in. There isn't anything that I can see messing it up.

I think it has something to do with me loading the file incorrectly. If I put in the text file the number 165, instead of getting a 165 I get a 48 or something. And then if the score 44 stores to the file, the file will hold a comma (which I conceive as conversion to a char somehow).

Any tips or help would be much appreciated!
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2725
  • View blog
  • Posts: 11,457
  • Joined: 20-September 08

Re: Serializable

Posted 09 June 2010 - 06:00 AM

Quote

If I put in the text file the number 165, instead of getting a 165 I get a 48 or something. And then if the score 44 stores to the file, the file will hold a comma (which I conceive as conversion to a char somehow).


Your problem is you're treating a binary file as a text file - your file extension gives this away.
Your program will work as long as your score is between 0 and 255. Just don't try to read it as a text file in a text editor. Using a hex editor will show you what's in there
Was This Post Helpful? 1
  • +
  • -

#14 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Serializable

Posted 09 June 2010 - 06:25 PM

So do I need to change the file extension? And if so to what?

Or do I need to change how I am reading it, like with a different reader? What do I need to change?
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Serializable

Posted 09 June 2010 - 06:34 PM

View Postzim1985, on 09 June 2010 - 07:25 PM, said:

So do I need to change the file extension? And if so to what?

Or do I need to change how I am reading it, like with a different reader? What do I need to change?

By the way you are not doing serialization here, you just write a binary file

What g00se said is you write binary numbers that will be interpreted as ASCII character by an editor like Notepad.

So if you write
int x = 65;

x into your file it will put 65 into it which is the ASCII code for letter A
so if you open the file with Notepad you will see "A"
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2