ObjectOutputStream Resulting in EOF Error - java

I'm trying to implement object serialization in java. I'm not sure why my code is currently leading to an EOFException, as my code is writing to the file it's supposed to:
Currently, my code is:
import java.io.*;
public class main {
public static void main(String[] args) {
product p1 = new product("product", 0, "this is a product.");
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("info.dat")));
out.writeObject(p1);
out.writeObject(p1);
System.out.println("Wrote to file");
} catch (FileNotFoundException e) {
System.out.println("");
} catch (IOException e) {
System.out.println("");
}
ObjectInputStream in = null;
try {
in = new ObjectInputStream(
new BufferedInputStream(
new FileInputStream("info.dat")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
while product class is extremely simple, being only:
import java.io.Serializable;
public class product implements Serializable {
String name;
double cost;
String description;
public product(String a,double b,String c) {
name = a;
cost = b;
description = c;
}
}
Currently, the output is
Meaning that my code did run and did write to file. I also added an extra write object just in case, so I really don't understand how an EOF error is occurring. Can somebody please explain why?

Related

Reading objects from assets

something is really messed up. I've got a ".ser" document in the assets folder, which stores an ArrayList of Objetcs. In an android application, I want to read this objects. There are a lot of posts related to this issue, however none of them could solve my problem. The strange part is, when I am using similar code in non - android context / "normal" java, it works properly. Here, the last line throws a NullPointerException - What is going wrong?
public void getData() {
ArrayList<MyClass> output= null;
InputStream is = null;
ObjectInputStream ois = null;
try{
is = this.getAssets().open("data.ser");
ois = new ObjectInputStream(is);
output = (ArrayList<MyClass>)ois.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.d("TAG", output.get(0).getId());
}
I would create a class and place the array within a single object:
public class ListObjects implements Serializable {
List<MyClass> listMyClass = new ArrayList<>();
public ListObjects(){
}
public List<MyClass> getListMyClass() {
return listMyClass;
}
public void setListMyClass(List<MyClass> listMyClass) {
this.listMyClass = listMyClass;
}
}
I had a similar problem. And it was because the name of the package in the java app was not called the same as the package name in android. And therefore I did not recognize them as equal objects. This is how I do it:
public static Object fromData(byte[] data) {
ObjectInputStream ois = null;
Object object = null;
try {
ois = new ObjectInputStream(
new ByteArrayInputStream(data));
object = ois.readObject();
} catch (Exception ex) {
Logger.getLogger(ModeloApp.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
ois.close();
} catch (Exception ex) {
Logger.getLogger(ModeloApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
return object;
}

Serialization:java.io.StreamCorruptedException: invalid stream header: 0AACED00

I'm a student practicing my File IO skills and I am coming up against a problem with reading Objects from a file using ObjectInputStream. The code is consistently throwing an InvalidClassException and I can't find how the code is throwing it online or by trial and error. Here's my code:
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class ReadFromFile {
String filename;
List<Object> os;
public ReadFromFile(String filename) {
this.filename = filename;
os = new ArrayList<>();
}
public Object[] readObject() {
try {
FileInputStream fis = new FileInputStream(filename);
ObjectInputStream ois = new ObjectInputStream(fis);
System.out.print("reading\n");
while (true) {
os.add(ois.readObject());
System.out.print("read one\n");
}
} catch (EOFException e) {
return os.toArray();
} catch (FileNotFoundException e) {
System.out.print("File not found\n");
return os.toArray();
} catch (ClassNotFoundException e) {
System.out.print("Class not found\n");
return os.toArray();
} catch (StreamCorruptedException e) {
System.out.print("SC Exception\n");
e.printStackTrace();
return os.toArray();
} catch (InvalidClassException e) {
e.printStackTrace();
System.out.print("IC Exception\n");
return os.toArray();
} catch (OptionalDataException e) {
System.out.print("OD Exception\n");
return os.toArray();
} catch (IOException e) {
System.out.print("IO Exception\n");
return os.toArray();
}
}
}
I wrote all of the separate catch blocks to figure out what Exception was being thrown and it always throws the InvalidClassException.
Here also is my Tree Class:
import java.io.Serializable;
public class Tree implements Serializable {
private static final long serialVersionUID = -310842754445106856L;
String species;
int age;
double radius;
public Tree() {
this.species = null;
this.age = 0;
this.radius = 0;
}
public Tree(String species, int age, double radius) {
this.species = species;
this.age = age;
this.radius = radius;
}
public String toString() {
return species + ", age: " + age + ", radius: " + radius;
}
}
And here is my write to file function:
public boolean write(Object object) {
try {
File f = new File(filename);
FileOutputStream fos = new FileOutputStream(f,true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object + "\n");
oos.close();
} catch (FileNotFoundException e) {
System.out.print("File Not Found\n");
return false;
} catch (IOException e) {
System.out.print("IOException\n");
return false;
}
return true;
}
Your knowledge is appreciated...
Stack trace:
SC Exception
java.io.StreamCorruptedException: invalid stream header: 0AACED00
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:806)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at ReadFromFile.readObject(ReadFromFile.java:17)
at WriteAndRecord.main(WriteAndRecord.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Process finished with exit code 0
java.io.StreamCorruptedException: invalid stream header: 0AACED00
This is caused by appending to the FileOutputStream. As I mentioned in a comment above, you can't append to a stream written by ObjectOutputStream, at least not without special measures. Keep the file and the ObjectOutputStream open until you've written all the objects you want to write, then close it, then deserialize from it.
NB As I also mentioned,
while ((object = in.readObect()) != null)
is not a valid object-reading loop. readObject() doesn't return null at end of stream: it throws EOFException. null can occur anywhere in the stream, any time you write one. The correct form of the loop is:
try
{
for (;;)
{
Object object = in.readObject();
// ...
}
}
catch (EOFException exc)
{
// end of stream
}
// other catch blocks ...
NB 2 This:
oos.writeObject(object + "\n");
should be just
oos.writeObject(object);
Otherwise you're implicity calling toString() and pointlessly appending a line terminator, so the result of readObject() will be a String, not the original object.
I think this was caused by the lack of a serialVersionUID.
Whenever you serialize an object, the ClassLoader needs something to verify the new loaded object against to verify it and ensure its compatibility. In order to do this, you just need to define a field in your class like this:
private static final long serialVersionUID = 12358903454875L;
Your IDE may have also given you a warning stating the lack of it (Eclipse does this).
This should solve your problem.
You can learn more in this excellent answer by Jon Skeet here: What is a serialVersionUID and why should I use it?.

How do I write multiple objects to the serializable file and read them when the program is used again?

I want to maintain database of users of a Bank for my project. I am able to save the number of users in one serializable file. But when I try to save the user to database it adds only the latest one to database.
Below is the sneak peak of code which writes the objects:
if(e.getSource()==submit) {
if(uFName != null && uLName != null && uInitialDeposit !=0) {
if(uAccountType=="Savings") {
Random randomGenerator = new Random();
//Gets the number of users from file if file exists
File f = new File(fileNameAdmin);
if(f.exists() && !f.isDirectory()) {
admin=db.readFromAdminDatabase();
}
u[admin.numberOfUsers]= new User();
u[admin.numberOfUsers].fName=uFName;
u[admin.numberOfUsers].lName=uLName;
u[admin.numberOfUsers].initalDeposit=uInitialDeposit;
u[admin.numberOfUsers].interestRate=uInterestRate;
u[admin.numberOfUsers].accountType="Saving";
u[admin.numberOfUsers].accountNumber=690000+admin.numberOfSavingsAccount;
//Generates a 4 digit random number which will be used as ATM pin
u[admin.numberOfUsers].atmPin=randomGenerator.nextInt(9999-1000)+1000;
//A savings account will be created
sa[admin.numberOfSavingsAccount]=new SavingsAccount(u[admin.numberOfUsers].accountNumber,u[admin.numberOfUsers].fName,u[admin.numberOfUsers].lName,
u[admin.numberOfUsers].initalDeposit,
u[admin.numberOfUsers].interestRate);
u[admin.numberOfUsers].sa=sa[admin.numberOfSavingsAccount];
System.out.println(u[admin.numberOfUsers].sa.balance);
JOptionPane.showMessageDialog(submit,"Congratulations! You are now a member of Symbiosis Bank."
+ "\nYour account number is "+u[admin.numberOfUsers].accountNumber
+" and your ATM Pin is "+u[admin.numberOfUsers].atmPin,"Account Created",JOptionPane.INFORMATION_MESSAGE);
try {
//for(int j = 0; j<admin.numberOfUsers; j++)
db.addUserToDatabase(u[admin.numberOfUsers]);
admin.numberOfSavingsAccount++;
admin.numberOfUsers++;
db.updateAdminDatabase(admin);
dispose();
setVisible(false);
//Welcome welcome = new Welcome();
//welcome.setVisible(true);
InitialInput back = new InitialInput();
back.setVisible(true);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
The database class which has functions to write to database:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Database implements Serializable {
String fileName = System.getProperty("user.home")+"/db.ser";
String fileNameAdmin = System.getProperty("user.home")+"/admindb.ser";
public void addUserToDatabase(User u){
FileOutputStream fos;
try {
fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(u);
oos.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
#SuppressWarnings("finally")
public User readFromUserDatabase() {
FileInputStream fis;
User temp = null;
try {
fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
temp = (User)ois.readObject();
//System.out.println(temp.fName);
ois.close();
}
catch(Exception e) {
e.printStackTrace();
}
finally {
return temp;
}
}
public void updateAdminDatabase(Administrator admin) {
FileOutputStream fos;
try {
fos = new FileOutputStream(fileNameAdmin);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(admin);
oos.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
#SuppressWarnings("finally")
public Administrator readFromAdminDatabase() {
FileInputStream fis;
Administrator temp = null;
try {
fis = new FileInputStream(fileNameAdmin);
ObjectInputStream ois = new ObjectInputStream(fis);
temp = (Administrator)ois.readObject();
//System.out.println(temp.fName);
ois.close();
}
catch(Exception e) {
e.printStackTrace();
}
finally {
return temp;
}
}
}
The code which is trying to read the database:
public void actionPerformed(ActionEvent e) {
if(e.getSource()==deposit) {
//Ask the amount to deposit
int userAmountToDeposit;
try {
for(int i = 0; i<=admin.numberOfUsers; i++) {
u[i] = db.readFromUserDatabase();
System.out.println(u[i].accountNumber);
}
for(int j =0; j<=admin.numberOfUsers; j++) {
if(u[j].accountNumber==userAccountNumber) {
if(u[j].atmPin==userPin) {
u[j].accountBalance=u[j].sa.balance;
u[j].sa.deposit(10);
u[j].accountBalance=u[j].sa.balance;
System.out.println(u[j].accountBalance);
}
}
}
}
Inorder to write and read multiple objects please try as below
Writing multiple object into List
WriteObject wo=new WriteObject(20, "Mohan");
WriteObject wo1=new WriteObject(21, "Mohanraj");
ArrayList<WriteObject> woi=new ArrayList<>();
try {
FileOutputStream fop=new FileOutputStream("c://object.ser");
ObjectOutputStream oos=new ObjectOutputStream(fop);
woi.add(wo);
woi.add(wo1);
oos.writeObject(woi);
} catch NotFoundException e) {
}
Reading all objects from file
try {
FileInputStream fis=new FileInputStream("C://object.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
WriteObject wo=null;
WriteObject[] woj=new WriteObject[5];
ArrayList<WriteObject> woi=new ArrayList<>();
woi=(ArrayList<WriteObject>)ois.readObject();
for(int i=0;i<woi.size();i++){
woi.get(i).getvalues();
}
Here getvalues() is method present in Writeobject class. Follow the same mechanism for your code snippet
If you want to fix it rapidly, you can create a list and store first and foremost your objects in the list (may be ArrayList or List), and then you'll save this list on your file. That is the nice method. Make sure that your objects are serializable.
below, listeVoitures is a stactic variable that will contain all
object that i'm going to retrive from file.
public static void saveVehiculeInFile(ArrayList vehiculeList) {
ObjectOutputStream ous = null;
//ArrayList<Vehicule> listVehiculeToSave = new ArrayList<>();
try {
ous = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(new File("garage.txt"))));
try {
ous.writeObject(vehiculeList);
System.out.println("\t=====> Les vehicules *** ont été ajouter dans le garage.");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ous != null) {
try {
ous.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
This method below is for retrive data from file
public static void readVehiculeFromFile() {
ObjectInputStream ins = null;
ArrayList<Vehicule> v = null;
try {
ins = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File("PoweredGarage.txt"))));
try {
v = (ArrayList<Vehicule>)ins.readObject();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (Vehicule vehicule : v) {
listeVoitures.add(vehicule);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (ins != null) {
try {
ins.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

Accessing a public static arrayList of a Thread Class from another Thread's Class

I am trying to access the
public static List<ChatThread> Chat_list of my ChatThread Class
from the run() method of my Client Class but i keep getting an empty array(Infact it throws an exception at that point : Exception in thread "Thread-2" java.lang.NullPointerException)
and am very certain that that arrayList exists and is not empty(Because i did a test on the arrayList in my ChatThread Class). Just take a look at my code.
Please I need your help on what to do.
Thanks.
This is the class containing the arrayList :
public class ChatThread extends Thread {
private Socket sc;
private String cherry_name;
private String passwd;
public static List<ChatThread> Chat_list = new ArrayList<ChatThread>(); //THE STATIC ARRAY LIST
private BufferedReader br;
private BufferedWriter bw;
public ChatThread(Socket sc){
try {
this.sc=sc;
System.out.println(sc);
br = new BufferedReader(new InputStreamReader(sc.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(sc.getOutputStream()));
String help = br.readLine();
this.cherry_name=help.split("#")[0];
this.passwd=help.split("#")[1];
System.out.println(this.cherry_name);
System.out.println(this.passwd);
Chat_list.add(this); //This is where i add it to the arrayList
if(Chat_list.isEmpty()) //This is where i did the test
System.out.println("I am empty");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
//Comparaison of information with that in the database
try{
bw.write("success");
bw.newLine();
bw.flush();
while(true){
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Socket getSc() {
return sc;
}
public String getCherry_name() {
return cherry_name;
}
}
As for the Client class :
public class Client extends Thread {
private Socket socket;
private BufferedReader br;
private BufferedWriter bw;
private ChatThread th;
private String cherry_name;
public Client(String cherry_name,String passwd){
try
{
socket = new Socket("127.0.0.1", 8888);
this.cherry_name=cherry_name;
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write(cherry_name+"#"+passwd);
bw.newLine();
bw.flush();
}
catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Erreur lors de la lecture du socket");
e.printStackTrace();
}
}
#SuppressWarnings("deprecation")
public void run()
{
try {
String help = br.readLine();
if(help.equals("failed")){
this.notify();
this.destroy();
socket.close();
}
else{
if(ChatThread.Chat_list.isEmpty()) System.out.println("Empty array!!!"); //This is where it says the array is empty whereas it wasn't the case in the ChatThread Class
for(ChatThread ct : ChatThread.Chat_list){
if(cherry_name.equals(ct.getCherry_name())){
th=ct;
break;
}
}
while(true){
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Error whilst reading from the socket");
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("Interruption");
e.printStackTrace();
}
}
public Socket getSocket() {
return socket;
}
}
And my server class :
public class Server {
public static void main(String[] args) {
try {
ServerSocket server =new ServerSocket(8888);
Socket sc;
System.out.println("Server Started");
while(true){
sc=server.accept();
System.out.println("New Connection");
new ChatThread(sc).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
A main class to instantiate the Client class :
public class help {
public static void main(String[] argv) {
new Client("Jerry","Smith").start();
}
}
Every access to a mutable object shared between two threads must be accessed in a synchronized way. Not synchronizing will lead to visibility and coherence issues like you're seeing.
You should not expose an ArrayList like that (even without multiple threads, public static mutable objects are already a very bad practice). Instead, you should encapsulate it in your own object, and make sure every access is synchronized properly.
It's hard to give a more concrete advice without seeing any line of your code.

Serialization of a class which doesnt implement Serializable

I have 2 classes serial1 and serial 2. serial1 implements Serializable whereas serial2 does not.As per theory i should get an Exception for the following code, but it is working fine. why is it so ?
import java.io.*;
public class SerialTest {
public static void main(String args[]){
FileOutputStream fos=null;
ObjectOutputStream oos =null;
serial1 se = new serial1();
serial1 sd = null;
se.mets();
try {
fos= new FileOutputStream("serialtest");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
oos =new ObjectOutputStream(fos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
oos.writeObject(se);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileInputStream fis=null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("serialtest");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ois = new ObjectInputStream(fis);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
sd = (serial1) ois.readObject();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sd.mets();
}
}
import java.io.Serializable;
public class serial1 implements Serializable{
/* public static void main(String []args){
serial1 ss = new serial1();
ss.mets();
}*/
public void mets(){
serial2 s2 = new serial2();
System.out.println( "serial 1 + mets");
s2.met1();
}
}
public class serial2 {
public void met1(){
System.out.println("serial2 + met1");
}
}
---------------------------*
The output is
serial 1 + mets
serial2 + met1
serial 1 + mets
serial2 + met1
You don't actually serialize serial2. Your mets method creates a local variable but as soon as the method returns it goes out of scope and becomes eligible for garbage collection.
If you had an instance variable of type serial2 inside serial1 then you would see an exception when you try to serialize (assuming it's a non-null value), but a local variable will not be a problem.
I don't see you ever serializing serial2

Categories