• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt in reading object file  RSS feed

 
adithya kavin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
this is my code
FileInputStream in=new FileInputStream("Data");
ObjectInputStream ois=new ObjectInputStream(in);
Person readPersonData = (Person)ois.readObject();

while((readPersonData = (Person)ois.readObject()) != null){
System.out.println(readPersonData.toString());


}
ois.close();
while reading an object file i am getting this error
name =vino
sex=f
name =ramar
sex=m
Exception in thread "main" java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2502)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1267)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:339)


please help me
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ObjectInputStream.readObject does not return null on EOF, as your while loop expects. It throws an EOFException. Have a look at the API documentation for the particulars, but I recommend that you store your various Person objects in a collection of some sort and serialize that. I don't like relying on exceptions to signal something that is expected, like EOF.
 
K Sathya Narayanan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i have class Filedata(Empid , empname)

i write this object in abc.txt file (only once)
when i try to read it in the next line i get the exception

Source Code;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;


public class ObjectStreamExample
{

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub

FileData f=new FileData(12,25);
System.out.println("Constructor");
System.out.println("f.EmpId ="+f.getEmpId());
System.out.println("f.InternalId ="+f.getInternalId());

try
{
WriteFileData(f);
FileData fun=(FileData) ReadFileData();

System.out.println("Read From File");
System.out.println("Fun.EmpId ="+fun.getEmpId());
System.out.println("Fun.InternalId ="+fun.getInternalId());

}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}


}


/* public static < T > void WriteGenericObjects(T u) throws IOException
{
String dataFile="abc.txt";
File file=new File("abc.txt");
System.out.println("1");
ObjectOutputStream out = new ObjectOutputStream(new
BufferedOutputStream(new FileOutputStream(file)));

out.writeObject(u);
System.out.println("2");
}*/

public static void WriteFileData(FileData f) throws IOException
{
String dataFile="abc.txt";
File file=new File(dataFile);
System.out.println("1");
FileOutputStream fos = new FileOutputStream("t.tmp");
ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(f);
oos.close();
System.out.println("2");
}

/*public static < T > T ReadGenericObjects() throws IOException, ClassNotFoundException
{
String dataFile="abc.txt";
ObjectInputStream out = new ObjectInputStream(new
BufferedInputStream(new FileInputStream(dataFile)));

T u=(T) out.readObject();

out.close();

return u;

}*/

public static FileData ReadFileData() throws IOException, ClassNotFoundException
{
String dataFile="abc.txt";
File file=new File(dataFile);
FileInputStream fis = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fis);

FileData u=(FileData) in.readObject();

return u;

}
}

class FileData implements Serializable
{
private int empId;

private transient int internalId;

public FileData(int empId, int internalId) {
//super();
this.empId = empId;
this.internalId = internalId;
}

/**
* @return the empId
*/
public int getEmpId()
{
return empId;
}

/**
* @param empId the empId to set
*/
public void setEmpId(int empId)
{
this.empId = empId;
}

/**
* @return the internalId
*/
public int getInternalId()
{
return internalId;
}

/**
* @param internalId the internalId to set
*/
public void setInternalId(int internalId)
{
this.internalId = internalId;
}

}
[ April 03, 2008: Message edited by: K Sathya Narayanan ]
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sathya,

what is "the exception"? In which line of code does it occur? Also, please UseCodeTags when posting code of any length. It's unnecessarily hard to read as it is.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!