PDA

View Full Version : Java Serialization


daniel_g
11-16-2006, 06:42 AM
I'm trying to learn java serialization, and I tried a code just like the one on this page:
http://java.sun.com/developer/technicalArticles/Programming/serialization/
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. Here is what my code looks like:


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


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);
.....

Does this mean Inv.dat has been created somewhere where I don't see it?
If so, where is Inv.dat being created or saved?

EDIT: 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.

ess
11-16-2006, 11:19 AM
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.

File directory = new File( System.getProperty( "user.home") );

if you are using Windows XP...the user's home directory would be
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.

File directory = new File( System.getProperty( "user.home") + File.separator + "Java" + File.separator + "CS211" + File.separator + "lab9" + File.separator + "persist" );

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.

File file = new File( directory, "Inv.dat" );

now...print out the path to the file just to be sure that is is located where you want to be.

System.out.println( "Path: " + file.getAbsolutePath() );

Good luck.
ess

daniel_g
11-16-2006, 04:53 PM
Thank you, that was a really nice reply, and now I'm able to save the file where I want.

I'm still wondering why it would save it on CS211 by default. I'm going to guess that making lab9 a package(sorry I didn't mention this earlier) has something to do with it, because other than that I don't see how CS211 was the default directory.