The exercise problem is to create a program that receives information from people, stores it in data, and watches it anytime
Vector vc = new Vector();
if (file.exists()){
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
vc = (Vector) ois.readObject();
ois.close();
}
This is how you do it, but the error is
Exception in thread "main" java.io.EOFException at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2335)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2804)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:802)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at Ex7.main(Ex7.java:11)
It comes out like this... I'm not sure what the problem is. It's a question from a book written by Kim Seung Hyun of Java It's like this even if I just type the sauce... Help me!!
vector
The Vector object is being read from a file, but it does not exist in that file.
Take a look at the following example. This is an example of writing three Vector's to a file and reading a Vector object from that file to ObjectInputStream three times. If you erase the 29th to 31st lines from this code, EOFException will occur the same way.
If you look at the bottom of the code, there's an "execute" button, so press it and run it on the code executor. It'll work fine. Then erase the 29th to 31st lines ( oos.writeObject(us1);
) and run them. The same exception occurs as the one you uploaded.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;
public class ObjectStream {
public static void main(String[] args){
// Store object files using ObjectOutputStream
FileOutputStream fos = null;
ObjectOutputStream oos = null;
// Create three objects by entering their name and age in UserClass.
Vector us1 = new Vector();
Vector us2 = new Vector();
Vector us3 = new Vector();
try{
// Creates an object output stream in the object.dat file.
fos = new FileOutputStream("object.dat");
oos = new ObjectOutputStream(fos);
// Write 3 objects sequentially in that file
oos.writeObject(us1);
oos.writeObject(us2);
oos.writeObject(us3);
// Completed writing 3 objects to the object.dat file.
System.out.println ("The object has been saved."");
}catch(Exception e){
e.printStackTrace();
}finally{
// Close the stream.
if(fos != null) try{fos.close();}catch(IOException e){}
if(oos != null) try{oos.close();}catch(IOException e){}
}
// Reads object data from a file.
FileInputStream fis = null;
ObjectInputStream ois = null;
try{
// Creates a stream that reads objects from the object.dat file.
fis = new FileInputStream("object.dat");
ois = new ObjectInputStream(fis);
// Read and output objects one by one from ObjectInputStream.
// A type transformation must be created with (User Class).
// System.out.println calls the object's implemented toString() function.
System.out.println( (Vector)ois.readObject());
System.out.println( (Vector)ois.readObject());
System.out.println( (Vector)ois.readObject());
}catch(Exception e){
e.printStackTrace();
}finally{
// Close the stream.
if(fis != null) try{fis.close();}catch(IOException e){}
if(ois != null) try{ois.close();}catch(IOException e){}
}
}
}
Code source: http://hyeonstorage.tistory.com/252
© 2024 OneMinuteCode. All rights reserved.