0

I have a project over data structures I am working on. I have a LinkedList and I need to be able to save object data to a text file

"Create a text file that is a list of last name, first name, and email address. You may use CSV or any other delimiter"

When I try to save the object data I get this "’ t John" instead of just "John"

My class(main and student, and email) implements Serializable

public class Main implements Serializable
{
    public static void main(String[] args) throws IOException
    {
    Date date = new Date();

    Email e1 = new Email("[email protected]");
    Email e2 = new Email("[email protected]");
    Email e3 = new Email("[email protected]");
    Student s1 = new Student("John", "Snow", e1);
    Student s2 = new Student("Shiba", "Inu", e2);
    Student s3 = new Student("Vern", "Vern", e3);
    Student s4 = new Student("Professor", "Messer", new Email("[email protected]"));
    ListInterface<Student> linkedList = new LinkedList<Student>();
    linkedList.add(s1, 2); //should be pos 3
    linkedList.add(s2, 3); //should be pos 4
    linkedList.add(s3, 1); // should be pos 2
    linkedList.add(s4, 1); //should be pos 1
    System.out.println(linkedList);
    try
    {
        FileOutputStream fos = new FileOutputStream("students.txt");
        ObjectOutputStream output = new ObjectOutputStream(fos);
        output.writeObject(s1.getfirstName());
        output.close();
        fos.close();
        System.out.println("Data saved in /file/students.txt");
    } 
    catch (IOException exc) 
        {
        System.out.println("Failed to write to file!");
        exc.printStackTrace();
        }
    }
}

Thanks!

2
  • 1
    The short answer is, you don't want to use an ObjectOutputStream Commented Oct 20, 2015 at 18:57
  • This is the second question of that csv topic within 20 minutes. Is there a major homework or test running? Commented Oct 20, 2015 at 19:03

1 Answer 1

1

If you want to write into a CSV file then you don't need to implement Serializable interface. With Serialization you can not write csv file. Serialization process write object state into .sar file. You may consider to use some CSV library or you may write it. You may check this tutorial.

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.