I want to write objects in human readable form in a text file, the file gets saved as a normal serialized object with unwanted characters instead.
How do I rewrite the program for saving into human readable text file?
import java.io.*;
class book implements Serializable
{
String name;
String author;
int nop;
int price;
int discount;
void getDiscount()
{
int finalprice=price-((price/discount));
System.out.println("Final price after discount="+finalprice);
}
public String toString()
{
return name+author+nop+price+discount;
}
}
class fileio
{
public static void main(String args[])
{
MainClass mainObject=new MainClass();
mainObject.writeToFile();
book javabook=new book();
javabook.name="Java unleashed";
javabook.author="someone";
javabook.nop=1032;
javabook.price=450;
javabook.discount=10;
javabook.getDiscount();
}
public void writeToFile()
{
try
{
File file=new File("JavaBook1.txt");
FileWriter fw=new FileWriter(file.getAbsoluteFile());
BufferedWriter bw=new BufferedWriter(fw);
bw.write(book.toString());
bw.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}