My code works fine with one object created by the book constructor and writes to both the object.dat files and the text.dat file, the problem is when I add more than one object, it overwrites (i guess) the file to only include that one object not both. I realy dont know what is going on with this and why its not writing both. The method to wirte the objects to the file works great (well i guess it does not) it does what I want it to do but it doesnt add the new objects to the file just overwirtes the old objects.
Did i forget some thing when making the object streams?
import java.io.*; import java.util.*; /** * @author Matthew Millar * NetBeans 6.7 * Windows Vista 64bit * January 30 2011 */ //This program is to test working with object and text data using streams and files public class Project1 { public static void main(String[] args) throws IOException { //three books to test prpogram with Book book1 = new Book("King Gus","Gus",450,1250.25); Book book2 = new Book("Ninja","Kim",780,55.62); book1.writeTextFile(book1); book1.writeObjectFile(book1); book2.writeObjectFile(book2); book2.writeTextFile(book2); try{ //read in file FileInputStream fis = new FileInputStream("Book-text.dat"); BufferedInputStream bis = new BufferedInputStream(fis); DataInputStream dis = new DataInputStream(bis); // dis.available() returns 0 if the file does not have more lines. while (dis.available() != 0) { // this statement reads the line from the file and print it to // the console. System.out.println("From Book-Text.dat"); System.out.println(dis.readLine()); } // dispose all the resources after using them. fis.close(); bis.close(); dis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //read object file try { //Construct the ObjectInputStream object ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("Book-objects.dat")); Object obj = null; while ((obj = inputStream.readObject()) != null) { if (obj instanceof Book) { System.out.println("From Book-Object.dat"); System.out.println(((Book)obj).toString()); } } inputStream.close(); } catch (EOFException ex) { //This exception will be caught when EOF is reached System.out.println("End of file reached."); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } } //books class class Book implements Serializable { //variables used private String title; private String author; private int numPage; private double price; //construtor //defult no param constructor Book(){} Book(String t, String a, int n, double p) { title = t; author = a; numPage = n; price = p; } //get methods public String getTitle() { return title; } public String getAuthor() { return author; } public int getPage() { return numPage; } public double getPrice() { return price; } public String toString() { return getClass().getName() + " Title: " + title + ", Author: " + author + ", Number of Pages: " + numPage + ", Price: " + price + "\n"; } //methods for writing files public void writeObjectFile(Object obj) { //write object to file try{ ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("Book-objects.dat")); outputStream.writeObject(obj); System.out.println("Object wrote " + obj); //clear the output stream outputStream.flush(); outputStream.close(); }catch(IOException ex) { ex.printStackTrace(); } } public void writeTextFile(Object obj) { try{ // Create file String textObj = obj.toString(); FileWriter fstream = new FileWriter("Book-text.dat"); BufferedWriter out = new BufferedWriter(fstream); System.out.println("Write Text File " + obj); out.write(textObj); //Close the output stream out.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } }//end of book class