I am developing a small application in Java. At certain point i need to save the object of my custom class to a text file for this i override toString() method in my custom class and then use ObjectOutputStream class to save object of my custom class to text file. Now everything works fine i.e my text file contains the text but along with that text it also contians some strange characters or raw data aswell. Following three lines contain major code for that
ObjectOutputStream outputStream = null;
outputStream = new ObjectOutputStream(new FileOutputStream(filename));
outputStream.writeObject(person);//person is the instance of my custom class
Is there any way to restrict that raw data to be saved in my text file?
An ObjectOutputStream serializes objects that you pass to writeObject(). It doesn’t write a text version of the object. If you want to write the value returned by toString() to a file, then open a file with something like a FileWriter, get the String by calling the toString() method, and then write it to the FileWriter.