Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-16-2007, 05:28 PM
Member
 
Join Date: Jul 2007
Posts: 26
paul is on a distinguished road
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.
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 ......
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 05:53 AM
Member
 
Join Date: Jul 2007
Posts: 39
coco is on a distinguished road
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.

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.

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.

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.

Code:
System.out.println( "Path: " + file.getAbsolutePath() );
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is Serialization and de-serialization in Java Java Tip java.io 0 04-04-2008 03:47 PM


All times are GMT +3. The time now is 01:54 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org