Search code examples
javaserializationfile-io

How to write Object as human readable text file


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();
        }

    }
}

Solution

  • See if below solves your purpose

    override toString() method of Object class to return your object's state and then write the output of it to text file with file writer

    If you want to have xml kind of representatio, go for JAXB approach

    Update:-

    please ignore syntax/compile errors in below program as i have not tested it but it will give you brief idea

    class Book
    {
        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;
        // above can be any format whatever way you want
    
    
        }
    }
    

    Now in your main class

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    
    class Fileio
    {
        public static void main(String args[])
        {
            Fileio mainObject=new Fileio();
    
            Book javabook=new book();
            javabook.name="Java unleashed";
            javabook.author="someone";
            javabook.nop=1032;
            javabook.price=450;
            javabook.discount=10;
            javabook.getDiscount();
            mainObject.writeToFile(javabook);
        }
            public void writeToFile(Book javabook)
            {
            try
            {
            File file=new File("JavaBook1.txt");
            FileWriter fw=new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw=new BufferedWriter(fw);
            bw.write(javabook.toString());
            bw.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }