Results 1 to 4 of 4
Thread: Java serialization
- 07-16-2007, 04:28 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 26
- Rep Power
- 0
Java serialization
I'm trying to learn java serialization, and I tried a code just like the one on this page
So I created my own code with my own objects.
The problem is that I specified to save the data into a file named "Inv.dat".
I have never actually seen that file being created, but, since I'm able to print the objects I added on the Write class from the Read class, my code works.
Java Code:public class Write{ public Write(){ String filename = "Inv.dat"; inventory.Inventory storeA = new inventory.Inventory (); ...... try{ FileOutputStream fos = new FileOutputStream(filename); ObjectOutputStream out = new ObjectOutputStream(fos); out.writeObject(storeA); out.close(); }// end try ......Does this mean Inv.dat has been created somewhere where I don't see it?Java Code:public class Read{ public static void main(String[] args){ String filename = "Inv.dat"; FileInputStream fis = null; ObjectInputStream ois = null; inventory.Inventory storeA = null; try{ fis = new FileInputStream(filename); ois = new ObjectInputStream(fis); storeA = (inventory.Inventory)ois.readObject(); }//end try ..... System.out.println(storeA); .....
If so, where is Inv.dat being created or saved?
Ah, never mind, I found it, now the question is why did it save it there? I've got the following directories:
MyDocuments > Java > CS211 > lab9 > persist
presist contains the Read/Write classes, and lab9 contains 2 other classes that create the objects. Inv.dat was created on the CS211 directory.
Thanks
- 08-07-2007, 04:53 AM #2
Member
- Join Date
- Jul 2007
- Posts
- 39
- Rep Power
- 0
Looking at your code...it does seem that you have specified a location of where the file should be saved...or it should be written.
In other words...if you do not explicitly specify a location...the file will be created and saved in the same directory of where the write class is located.
If you want to explicitly specify a location...the best way to do it...is to write an instance of the File class which would encapsulate the directory that you wish to write in. Then, create another instance of the File class which would encapsulate the file that you wish to write to.
Here is an example. In this example, I am going to create a file instance that represents the user's home directory.
if you are using Windows XP...the user's home directory would beJava Code:File directory = new File( System.getProperty( "user.home") );
C:\Documents and Settings\user_name
If you are using Linux..the user's home directory would be.
/home/user_name
Assuming that the following directory structure exists in your home directory MyDocuments -> Java -> CS211 -> lab9 -> persist, we could re-write the directory instance to encapsulate that directory....regardless of what operating system you are using.
Here is the best part...now you can create an instance of the File class that should encapsulate the file that you wish to write to.Java Code:File directory = new File( System.getProperty( "user.home") + File.separator + "Java" + File.separator + "CS211" + File.separator + "lab9" + File.separator + "persist" );
now...print out the path to the file just to be sure that is is located where you want to be.Java Code:File file = new File( directory, "Inv.dat" );
Java Code:System.out.println( "Path: " + file.getAbsolutePath() );
- 04-10-2009, 08:38 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 15
- Rep Power
- 0
what is the use of defaultwriteobject() in serialization? how does it differ from writeobject() ?
- 04-10-2009, 08:58 PM #4
When you are writing writeObject(ObjectOutputStream) for your own classes you can call defaultWriteObject() to call the default method. There is also the method ObjectOutputStream.writeObject(Object), which is what you actually call to do the writing.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
Similar Threads
-
What is Serialization and de-serialization in Java
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks