jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Appending an object to a file does not work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Appending an object to a file does not work" Watch "Appending an object to a file does not work" New topic

Appending an object to a file does not work

shekar bairi

Joined: Jul 02, 2007
Posts: 3
This is the file which performs the logic

public class ReadWriteDataTest {
private static void storeuserData(File f, UserDetails userDetails)
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(f,true));
//ObjectOutput out = new ObjectOutputStream(new FileOutputStream(f));
} catch (Exception e)
System.out.println("Unable to store users, exception : " + e.getMessage());

public static void main(String[] args) {

UserDetails user = new UserDetails();

UserDetails user2 = new UserDetails();

try {
File file = new File("Test.txt");
//storeuserData(file, user);
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(file));
//ObjectOutput out = new ObjectOutputStream(new FileOutputStream(f));
//storeuserData(file, user3);
FileOutputStream fos = new FileOutputStream(file,true);
out = new MyObjectOutputStream(fos);
//ObjectOutput out = new ObjectOutputStream(new FileOutputStream(f));

UserList list = (UserList)getUserList(file);
UserDetails user1 =null;

//user1 = (UserDetails)getUserData(file);

for (int i=0;i<list.size(); i++)
user1 = list.get(i);
System.out.println("Name : "+user1.getUserName());
System.out.println("password : "+user1.getPassword());
System.out.println("LName : "+user1.getLName());
System.out.println("FName : "+user1.getFName());
System.out.println("Address : "+user1.getAddress());
System.out.println("City : "+user1.getCity());
System.out.println("Zip : "+user1.getZip());
System.out.println("PhNumber : "+user1.getPhNumber());
System.out.println("FxNumber : "+user1.getFxNumber());
System.out.println("Email : "+user1.getEmail());
System.out.println("Objective : "+user1.getObjective());

} catch(Exception e) {



This is the extended file

package bo;

import java.io.*;

class MyObjectOutputStream extends ObjectOutputStream

MyObjectOutputStream(OutputStream out) throws IOException

protected void writeStreamHeader() throws IOException

I get an error when I run this file. Can someone please help me..
The error is as follows

D:\Tomcat 5.5\webapps\wecare\source>java bo/ReadWriteDataTest
Exception in thread "main" java.lang.NoSuchMethodError
at bo.ReadWriteDataTest.main(ReadWriteDataTest.java:101)
[ July 02, 2007: Message edited by: shekar bairi ]
Francesco Bianchi
Ranch Hand

Joined: Jun 22, 2007
Posts: 62
Two things: by the line number I cannot understand where the problem exactly occurs..could you underline it somehow in the source you have posted please? An obvious check: is the UserDetails class implementing the Serializable interface?

SCJP 5 & 6, SCWCD 5, SCBCD 5
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15100

Note: Please use code tags when you post source code - it makes your source code much easier to read. Thank you!

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Please make an effort to use CODE tags and to cut your code down to the minimum that demonstrates the problem. For instance, your posted code has commented-out lines, which do nothing but confuse the reader.

That said, I think I may be able to help with the issue. I think you write objects to a stream in two distinct stages, using separate ObjectOutputStreams, the second appending to a file that was fully written by the first. Then I think you try to read it in a single stage, using one ObjectInputStream to read both objects. That won't work; the input and output must be done the same way.

However, the posted error is NoSuchMethodError, which is generally an indication of a mis-build. If a required method was not present at compile time, you'd have got a compiler "cannot find symbol" error, not a run-time NoSuchMethodError. This suggests that one of your classes has been modified to be incompatible with another class, but you haven't done enough recompiling to find out.

So you should sort out your build, making sure all classes are compatible with each other, before trying to debug any problem with the serialisation.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
shekar bairi

Joined: Jul 02, 2007
Posts: 3

This is the code.

Objective : I dont have a database hence I plan to write the user details in a text file. For the user details I have a class (UserDetails). I also have a class UserList which stores all the details of the users in a ArrayList.

I am trying to append one user at a time when the user enters his details in a jsp and clicks on submit(The jsp code is not pasted here). For that I sensed that the new user detail should get appended at the end which is not happening.

Hence inorder to test I added a main method and tried to enter 2 records simultaneously by putting the append value true. Please refer to this line in the code (ObjectOutput out = new ObjectOutputStream(new FileOutputStream(f,true));.

The error occurs here.

1>Can I append the objects at the end of the file?
2>From the previous post I sensed that reading also would be a problem. Do I need to do anything different to read all the users from the file.

Thanks for the previous replies
I agree. Here's the link: http://aspose.com/file-tools
subject: Appending an object to a file does not work
It's not a secret anymore!