Results 1 to 4 of 4
  1. #1
    paul is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

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

    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

  2. #2
    coco is offline Member
    Join Date
    Jul 2007
    Posts
    39
    Rep Power
    0

    Default

    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.

    Java Code:
    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.

    Java Code:
    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.

    Java Code:
    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.

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

  3. #3
    vijay24805 is offline Member
    Join Date
    Mar 2009
    Posts
    15
    Rep Power
    0

    Default

    what is the use of defaultwriteobject() in serialization? how does it differ from writeobject() ?

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  1. Replies: 0
    Last Post: 04-04-2008, 02:47 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
  •