Reputation: 343
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!
Upvotes: 0
Views: 3952
Reputation: 11163
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.
Upvotes: 1