Results 1 to 4 of 4
- 07-25-2009, 08:22 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 1
- Rep Power
- 0
error in reading objects from file
Below is a short code to explain the problem i am facing
i have been working on the same for past one week i am facing exceptions while reading a file containing more than one objects
please i would be grateful if u could reply as early as possibleJava Code:import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author sakshi */ public class Storingobject implements Serializable{ int x,y; public void addObject() { //Storingobject temp; ObjectOutputStream objOut=null; try { objOut = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("temp.dat")));//if i pass true as parameter in FileOutputStream it gives Stream Corrupted exception } catch (IOException ex) { Logger.getLogger(Storingobject.class.getName()).log(Level.SEVERE, null, ex); } try { // while((temp=objOut.writeObject(o))!=null) objOut.writeObject(this); System.out.println("Saved.."); objOut.close(); } catch (IOException ex) { Logger.getLogger(Storingobject.class.getName()).log(Level.SEVERE, null, ex); } } public void readobject() { int i=1; Storingobject temp; ObjectInputStream objIn=null; try { objIn = new ObjectInputStream(new BufferedInputStream(new FileInputStream("temp.dat"))); } catch (IOException ex) { Logger.getLogger(Storingobject.class.getName()).log(Level.SEVERE, null, ex); } try { while ((temp = (Storingobject) objIn.readObject()) != null) { System.out.println("VAlues of object i++ :"); System.out.println("X: "+temp.x+"Y: "+temp.y); } } catch (IOException ex) { Logger.getLogger(Storingobject.class.getName()).log(Level.SEVERE, null, ex); } catch (ClassNotFoundException ex) { Logger.getLogger(Storingobject.class.getName()).log(Level.SEVERE, null, ex); } } public static void main(String args[]) { Storingobject o1=new Storingobject(); o1.x=10;o1.y=15; o1.addObject(); o1.readobject(); } }
- 07-27-2009, 06:27 AM #2
Member
- Join Date
- Jul 2009
- Posts
- 10
- Rep Power
- 0
i modified your main method:
there are two objects, but i found no error.Java Code:public static void main(String args[]) { StoringObject o1 = new StoringObject(); o1.x = 10; o1.y = 15; o1.addObject(); o1.readobject(); StoringObject o2 = new StoringObject(); o2.x = 20; o2.y = 25; o2.addObject(); o2.readobject(); }-----------------------------
executes everything
- 07-27-2009, 06:29 AM #3
Member
- Join Date
- Jul 2009
- Posts
- 10
- Rep Power
- 0
reply
i modified your main method:
there are two objects, but i found no error.Java Code:public static void main(String args[]) { StoringObject o1 = new StoringObject(); o1.x = 10; o1.y = 15; o1.addObject(); o1.readobject(); StoringObject o2 = new StoringObject(); o2.x = 20; o2.y = 25; o2.addObject(); o2.readobject(); }-----------------------------
executes everything
- 07-27-2009, 08:17 AM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
StreamCorruptedException? odd, I would have though the constructors would be safe from that, but apparently not. Rather unlikely that it has anything to do with the filename, but try just in case the .dat extension is causing problems for the constructor with a boolean.
Try a static variable to track number of objects written. i.e
Code is untested and may have errorsJava Code:private static int numObjs=0; public void addObject(){ StoredObject[] orig = new StoredObject[numObjs]; //read objects and store in an array try { objIn = new ObjectInputStream(new BufferedInputStream(new FileInputStream("temp.dat"))); } catch (IOException ex) { Logger.getLogger(Storingobject.class.getName()).log(Level.SEVERE, null, ex); } if(objIn!=null){ for(int i=0;i<numObjs;++i){ orig[i]=(StoredObject) objIn.readObject(); //check if object read is null and do error handling to solve problem for later writes if(orig[i]==null){ numObjs=i - 1; } } } //write objects to file (iterate through the array using another for loop) //then write the new object ++numObjs; }If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Similar Threads
-
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-09-2009, 11:31 PM -
Error Message when reading an input file.
By Deluyxe in forum New To JavaReplies: 8Last Post: 04-26-2009, 04:02 PM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM -
error in reading mail contents
By kib_tse in forum NetworkingReplies: 1Last Post: 08-22-2008, 12:14 AM -
reading from a zip file, error
By Mr tuition in forum AWT / SwingReplies: 1Last Post: 01-16-2008, 12:39 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks