2 Replies - 1469 Views - Last Post: 16 November 2010 - 06:42 PM Rate Topic: -----

#1 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Reading an object from a binary file

Posted 16 November 2010 - 02:37 PM

I'm trying to use ObjectInputStream to read an object from a binary file that I wrote to with ObjectOutputStream. I tried using the .readObject() method but it only reads Objects, not the student object that I made. I tried casting it to Student but then it only reads the studentID and major field. My Student class inherent from a person class that I made, person holds the first and last name. Heres my code:

The WriteFile class:
import java.io.*;
import java.util.*;

import javax.swing.JOptionPane;

public class WriteFile implements Serializable{
	private FileInputStream fis;
	private ObjectInputStream ois;
	private FileOutputStream fos;
	private ObjectOutputStream oos;
	private ArrayList<Student> list;
	
	public WriteFile(String f, ArrayList<Student> a) {
		list = new ArrayList<Student>(a.size());
		
		//performs a deep copy
		for(int i=0; i<a.size(); i++) {
			this.list.add(i, a.get(i));
		}
		
		try {
			fos = new FileOutputStream(f);
			oos = new ObjectOutputStream(fos);
			fis = new FileInputStream(f);
			ois = new ObjectInputStream(fis);
		}
		catch(FileNotFoundException e) {
			JOptionPane.showMessageDialog(null, "File not found");
			System.exit(0);
		}
		catch(IOException e) {
			JOptionPane.showMessageDialog(null, "Could not make a connect with the file");
		}
	}
	
	public void write(Student s){
		try {
			oos.writeObject(s);
			
			oos.close();
		}
		catch(IOException e) {
			JOptionPane.showMessageDialog(null, "An error has occured");
		}
	}
	
	public void write(int i){
		try {
			oos.writeObject(list.get(i));
			
			oos.close();
		}
		catch(IOException e) {
			JOptionPane.showMessageDialog(null, "An error has occured");
		}
	}
	
	public Student read() throws IOException, ClassNotFoundException {
		return (Student) ois.readObject();
	}
}




My driver class:
import java.io.*;
import java.util.*;

public class TestClass {
	
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		ArrayList<Student> list = new ArrayList<Student>(0);
		int i = 0;
		
		while(i < 5) {
			list.add(i, new Student("John", "Smith", "777555333", "Comp Sci"));
			i++;
		}
		
		WriteFile file = new WriteFile("students.dat", list);
		file.write(0);
		
		list.add(file.read());
		System.out.println(list.get(5));
	}

}




Heres my output:
First Name: 
Last Name: 
Student ID: 777555333
Major: Comp Sci




Is This A Good Question/Topic? 0
  • +

Replies To: Reading an object from a binary file

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,987
  • Joined: 20-September 08

Re: Reading an object from a binary file

Posted 16 November 2010 - 04:21 PM

Quote

Heres my output:


What's wrong with that?
Was This Post Helpful? 0
  • +
  • -

#3 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: Reading an object from a binary file

Posted 16 November 2010 - 06:42 PM

View Postg00se, on 16 November 2010 - 03:21 PM, said:

Quote

Heres my output:


What's wrong with that?


First Name, and Last Name are empty. I figured out my problem though. I didn't implement Serializable on my person class but I did on my student class.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1