java loading data from file

Soldato
Joined
2 Oct 2004
Posts
4,362
Location
N.W London
Hi Guys

I am trying to restore the state of a program using JAVA which I saved in a file.

When the user clicks on "restore" the program should overwrite the existing object with the data from the object stored in the file.

Here is what I have:


public Record restore(String file)
{
try
{
FileInputStream ins = new FileInputStream(file);
ObjectInputStream outs = new ObjectInputStream (ins);

Artist loadArtists = (Artist)outs.readObject();
outs.close();
return loadArtists;
}
catch (FileNotFoundException e) {System.out.println (e);}
catch (IOException e) {System.out.println (e);}
catch (ClassNotFoundException e) {System.out.println (e);}

return null;
}

This code reads in the object saved to file and returns it but does not overwrite the existing object.

I dont know why?

Can you help me overwrite the existing object with the object I retrieve from the file?

Thanks in advance for any help I receive

Cheers
 
I'm not a java programmer so excuse me if any of this is incorrect...

Where is your existing object? Don't you just need to do:

existingObject = restore(myfile);

Or do you want restore to be a method on your object?
 
Oh OK. You're reading the object from the file into 'loadArtists', so you could assign each property of loadArtists that you're interested in to your current object. So in the restore function on your object you could do:

this.property1 = loadArtists.property1;
this.property2 = loadArtists.property2;
etc..
 
but I have to "cast" the data I read in don't I?

I thought I could read in the entire object and simply overwrite the existing one?

Please advise

thanks for your help
 
I'll just write what I think you're trying to achieve to make sure I'm not totally misunderstood...

Artist is your class, and restore is a function on that class. You want to call restore on an instance of Artist, which will assign values from a file to that instance of Artist?

If that's the case, you're casting the data you read in into an Artist object already? So the loadArtists object is already in the correct data structure? You can just assign its values to the instance of the object that restore is being called on?

Sorry, I don't know enough about Java to be a real help! I'm sure there'll be some clever guys on here soon who can help you further.
 
Here's a quick example of what I think you're trying to do:

I have a class called Artist that has attributes first and last name:

initialise Artist Object a with the name "New Artist":
then call my ReadDataFromFile class with the name of the file which contains the new name and the object a that I want to overwrite with the values from the file.


Code:
public class Main {

	public static void main(String[] args) {
		
                // Initialise Artist Object
		Artist a = new Artist("New","Artist");
		a.getFullName();

		ReadDataFromFile rdff = new ReadDataFromFile();

		rdff.readInputFile("/Data/a1", a);
                //check if Artist Object is updated with new values
		a.getFullName();

	}

}


Code:
public Artist readInputFile(String f, Artist a) {
		
                // Read new name from file
		Scanner s = new Scanner(new InputStreamReader(getClass().getResourceAsStream(f)));
		
		while (s.hasNextLine()) {
                        // Update Artist Object a with new name
			a.setFname(s.nextLine());
			a.setLname(s.nextLine());
		}
			
		s.close();
		
                // return updated Artist object
		return a;	
		
	}

Jar file with code: https://dl.dropboxusercontent.com/u/34734541/objectOverwrite.jar

To test out my code run:
Code:
java -jar objectOverwrite.jar

I've left the source files in the jar file so you can pursue at your lesiure.
 
thank you so much for this is worked a treat....

basically all I had to do was define the fields I was currently using and then pass the data into them from the objects stored in my file.

really appreciate all your help

cheers guys :-)
 
Back
Top Bottom