1

Beginner in Java, Making a program of student Management in which there is class student, I wanted to save the data of students , as an object to a text File .I tried using Objectoutputstream ,but the contents in the File are shown in some awkward form .Any Help would be greatly Appreciated.

1
  • 1
    What do you want to save it for? Do you want to load it into the program again at a later time, or should the file be human-readable, like XML? Commented Jun 29, 2014 at 10:42

1 Answer 1

3

You can try the below example code to use ObjectOutputStream in Java in order to write it in the file :

import java.io.Serializable;

public class Person implements Serializable {

    private String firstName;
    private String lastName;
    private int age;

    public Person() {
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    //Overriding toString to be able to print out the object in a readable way
    //when it is later read from the file.
    public String toString() {

        StringBuffer buffer = new StringBuffer();
        buffer.append(firstName);
        buffer.append("\n");
        buffer.append(lastName);
        buffer.append("\n");
        buffer.append(age);
        buffer.append("\n");

        return buffer.toString();
    }


}

This is the code for creating instances of the Person class and writing them to an ObjectOuputStream:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class Main {

    /**
     * Example method for using the ObjectOutputStream class
     */
    public void writePersons(String filename) {

        ObjectOutputStream outputStream = null;

        try {

            //Construct the LineNumberReader object
            outputStream = new ObjectOutputStream(new FileOutputStream(filename));

            Person person = new Person();
            person.setFirstName("James");
            person.setLastName("Ryan");
            person.setAge(19);

            outputStream.writeObject(person);

            person = new Person();

            person.setFirstName("Obi-wan");
            person.setLastName("Kenobi");
            person.setAge(30);

            outputStream.writeObject(person);


        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            //Close the ObjectOutputStream
            try {
                if (outputStream != null) {
                    outputStream.flush();
                    outputStream.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        new Main().writePersons("myFile.txt");
    }
}

Hope you got the clear cut idea and sample code.

Thank you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.