Results 1 to 4 of 4
  1. #1
    Wazniak is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default ObjectContainer - serialized objects

    Hey, I need a little help with a project (my code below). I need to write an ObjectContainer class which implements interface Container, it's task is to write and read serialized objects to disk. Object has to be saved to the disk and acquire unique ID (int) to retrieve it. ID is returned to the user.
    Additional Info: System is supposed to handle more than 1 objetct, I can't modify Container interface.
    My main problem is that I don't know how make each object to be written to a different file (maybe also different directory) and well I probably just don't understand the whole task well enough.
    I commented out some ideas i've tried.

    Java Code:
    import java.io.*;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    
    interface Container {
    
       public int save( String path, Object objToSave );
       public Object read( int objToRead );
    }
    
     class ObjectContainer implements Container, Serializable  {
    
    	Map<Integer, String> map = new HashMap <Integer, String> ();
    	private int id = 0;
    	//Iterator<Map.Entry<Integer,String>> entries = map.entrySet().iterator();
    	
    	
    	@Override
    	public int save(String path, Object objToSave) {
    		
    		try{
    			/*File file = new File(path);
    			if(!file.exists()) file.mkdirs();
    			for (File child : file.listFiles()) {*/	 
    	    		//	FileOutputStream fout = new FileOutputStream(path);
    				
    				//	if (file.isDirectory());
    					
    	    			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("sth.txt"));   // (path) ?
    	    			oos.writeObject(objToSave);
    	    			oos.close();
    	    			System.out.println("Everything went better than expected! "+ map.put(id, path));
    			 
    			}
    		
    	    	  catch(Exception ex){
    	   		   ex.printStackTrace();
    	   	   }
    	  	  
    	   
    	
    return id;
    	}
    	public Object read(int objToRead) {
    		
    		  try{
    			  
    			 //  FileInputStream fin = new FileInputStream(path);
    			  String ref = map.get(objToRead);
    			   ObjectInputStream ois = new ObjectInputStream(new FileInputStream("sth.txt"));  // (path)?
    			   try{
    				Object obj = (Object)ois.readObject();
    				   return obj;
    			   }
    			  finally{
    				  ois.close();
    			  }
    	 
    			 //  return obj;
    	 
    		   }catch(Exception ex){
    			   ex.printStackTrace();
    			   return null;
    		   } 
    		  
    
    		
    	}
    
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: ObjectContainer - serialized objects

    The save method still looks not bad (without the comments :D). You have to pass the path string to the fileoutputstream, instead of "sth.txt".
    And after the writeObject, you have to add the id + path to your map and increment the id counter.

    The read method still looks not bad too. Here the same, instead of pass "sth.txt" to the FileInputStream, you have to pass the string (ref) you've been getting previously from the map.

  3. #3
    Wazniak is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Re: ObjectContainer - serialized objects

    Thanks a lot for help, i've changed it according to your suggestions. However I still don't know how to use some kind of iterator (I guess i need to if I have to increment ID + handle many objects ) I've been trying with
    Java Code:
    while(entries.next() != null)
    loop but it's probably not the best idea.

    Slightly changed code:

    Java Code:
    import java.io.*;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    
    interface Container {
    
       public int save( String path, Object objToSave );
       public Object read( int objToRead );
    }
    
     class ObjectContainer implements Container, Serializable  {
    
    	Map<Integer, String> map = new HashMap <Integer, String> ();
    	private int id = 0;
    	Iterator<Map.Entry<Integer,String>> entries = map.entrySet().iterator();
    	
    	
    	@Override
    	public int save(String path, Object objToSave) {
    	
    	/*File file = new File(path);
    			if(!file.exists()) file.mkdirs();
    			for (File child : file.listFiles()) {*/	 
    
              //while(entries.next() != null){
    		try{
    			
    	    						
    	    			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
    	    			oos.writeObject(objToSave);
                                    map.put(id,path);
    	    			oos.close();
    	    			System.out.println("Everything went better than expected! "+ map.put(id, path));
                                    id++;
    			 
    			}
    		
    	    	  catch(Exception ex){
    	   		   ex.printStackTrace();
    	   	   }
    	  	// }  
    	   
    	
    return id;
    	}
    	public Object read(int objToRead) {
    		
    		  try{
    			  
    			 
    			  String ref = map.get(objToRead);
    			   ObjectInputStream ois = new ObjectInputStream(new FileInputStream(ref));
    			   try{
    				Object obj = (Object)ois.readObject();
    				   return obj;
    			   }
    			  finally{
    				  ois.close();
    			  }
    	 
    			
    		   }catch(Exception ex){
    			   ex.printStackTrace();
    			   return null;
    		   } 
    		  
    
    		
    	}
    
    }

  4. #4
    Wazniak is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Re: ObjectContainer - serialized objects

    Thanks for help, problem solved :)

Similar Threads

  1. writing and Reading a serialized java object
    By KunCode in forum New To Java
    Replies: 2
    Last Post: 05-20-2012, 06:51 PM
  2. Replies: 3
    Last Post: 10-23-2011, 08:18 PM
  3. Serialized Java Obj testing
    By akokane in forum New To Java
    Replies: 0
    Last Post: 02-02-2011, 06:38 AM
  4. Size of serialized object with null values
    By jamesd128 in forum New To Java
    Replies: 1
    Last Post: 10-22-2010, 06:43 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
  •