When you send data on an object stream, it will send each object only once. This means if you modify and object and send it multiple times, you need to either use writeUnshared(mutableObject) or reset() which clears the cache of objects already sent.
You can't create an ObjectOutput/InputStream repeatly. If you want to make sure data is sent instead of buffered use flush(). If you are sending data like int instead of Objects, try DataOutput/InputStream.
java - Why does ObjectOutputStream.writeObject not take a Serializable? - Stack Overflow
java - Why does ObjectOutputStream write symbols to a file instead of my input? - Stack Overflow
Slow object persistance using objectoutputstream.writeobject
java - difference between "DataOutputStream" and "ObjectOutputStream" - Stack Overflow
Videos
When you send data on an object stream, it will send each object only once. This means if you modify and object and send it multiple times, you need to either use writeUnshared(mutableObject) or reset() which clears the cache of objects already sent.
You can't create an ObjectOutput/InputStream repeatly. If you want to make sure data is sent instead of buffered use flush(). If you are sending data like int instead of Objects, try DataOutput/InputStream.
See the Javadoc for ObjectOutputStream.reset() and ObjectOutputStream.writeUnshared().
This is because writeObject in ObjectOutputStream overrides the method in the ObjectOutput interface which does not require that the object be Serializable.
The ObjectOutput interface specifies methods that allow objects to be written to a stream or underlying storage, but this may be achieved by a process other than serialization. The ObjectOutputStream implements this functionality, but requires serializable objects. However, it cannot modify the signature of the interface that it implements.
It should be ObjectOutputStream.writeObject(serializable) rather than ObjectOutputStream. writeObject(Object). It is a proper use case where a marker interface like Serializable should have been used but unfortunately not. This would have made it possible the very real benefit of compile-time type checking instead of failing at runtime if the object does not implement Serializable interface.
I would like to take this opportunity to mention what Joshua Bloch has mentioned in his book Effective java:
A marker interface is an interface that contains no method declarations, but merely designates (or “marks”) a class that implements the interface as having some property. For example, consider the Serializable interface. By implementing this interface, a class indicates that its instances can be written to an
ObjectOutputStream(or “serialized”).In the case of the
Serializablemarker interface, theObjectOutputStream.write(Object)method will fail if its argument does not implement the interface. Inexplicably, the authors of theObjectOutputStreamAPI did not take advantage of theSerializableinterface in declaring the write method. The method’s argument type should have beenSerializablerather thanObject. As it stands, an attempt to callObjectOutputStream.writeon an object that doesn’t implementSerializablewill fail only at runtime, but it didn’t have to be that way.
What you've done is serialize a Java object to a file with ObjectOutputStream. That means, Java transforms your object Clients into a specific representation, which covers all the state your object has, e.g. member variables. This representation is not meant to be human-readable, it is a binary format, which is more efficient and suitable for classes and the Java enviroment, than a text format. Look here for more information.
What you can do instead is use a FileWriter, BufferedWriter or PrintWriter, which have multiple write... methods to write text to a file. In this case, you have to write each variable for its own as String, etc., not the whole class. Here you can an find example on that.
Another option to do the same as ObjectOutputStream for a whole object but in a completely customizable fashion and human-readable form is to use the Java Architecture for XML Binding (JAXB), but this requires a bit of reading. There are also other object serialization libraries for JSON and other formats.
Why is it when I run this java program it creates the text file but it prints out symbols instead of my input?
Because it isn't a text file. It is serialized data, written according to the Java Serialization Protocol, because you're using ObjectOutputStream.writeObject().
That also means that CustomerLists.txt is not an appropriate name for this file.
If you want text, use PrintWriter or BufferedWriter.
I suggest to do it this way
ObjectOutputStream o1 = new ObjectOutputStream(new FileOutputStream("1"));
... write objects
o1.close();
ObjectOutputStream o2 = new AppendingObjectOutputStream(new FileOutputStream("1", true));
... append objects
o2.close();
it definitely works.
import EJP;
import Evgeniy_Dorofeev;
public class answer
{
private String answera, answerb;
public answer(String a, String b)
{
answera = a;
answerb = b;
}
public void main(String[] args)
{
answer(EJP.response(), Evgeniy_Dorofeev.response());
System.out.println(answera + '\n' + answerb);
}
}