Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMatthew Francis
6,967 PointsNewbie - Serialization, Why not use system.out.printf() instead of PrintWriter.printf()?
public void exportTo(String fileName){
try(
fileOutputStream fos = new FileOutputStream(fileName);
PrintWriter writer = new PrintWriter(fos);//PrintWriter prints formatted representations of objects to a text-output stream.,
we used ToString(), so printWriter works
){
for(Song song : mSongs){
writer.printf("%s|%s|%s %n", song.getArtist(), song.getTitle(). song.getVideoUrl());
//Why not use system.out.printf()? Why use PrintWriter.printf()?
}
}catch(IOException ioe){
System.out.printf("Problem saving %s %n", fileName);
ioe.printStacktrace();
}
}
3 Answers
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsBecause writer.printf
writes to file(with all syntax above), whereas system.out.printf
writes to console. Different printf-s for different purposes.
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsNo I think, no serialization here. when you write:
writer.printf("%s|%s|%s %n", song.getArtist(), song.getTitle(). song.getVideoUrl());
All you get: is simple line "Artist|Title|VideoUrl" in file. Text file.
If you want to save something using serialization, then you have to use the code, like here:
http://www.tutorialspoint.com/java/java_serialization.htm
import java.io.*;
public class SerializeDemo
{
public static void main(String [] args)
{
Employee e = new Employee();
e.name = "Reyan Ali";
e.address = "Phokka Kuan, Ambehta Peer";
e.SSN = 11122333;
e.number = 101;
try
{
FileOutputStream fileOut =
new FileOutputStream("/tmp/employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/employee.ser");
}catch(IOException i)
{
i.printStackTrace();
}
}
}
You see how ObjectOutputStream
is used. I haven't tried that myself, but I do believe this code, and in the video, Crais save files just as text
Colby Wise
3,165 PointsAlexander - so to help me break this down into layman's terms: you use serialization to [read / write] text files / data? Or what's the big idea or purpose for it. What are the benefits of someone implementing serialization?
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsTon of resources about serialization out there.
Here is nice one, e.g.
http://stackoverflow.com/questions/2232759/what-is-the-purpose-of-serialization-in-java
Colby Wise
3,165 PointsAs always, thanks AN
Matthew Francis
6,967 PointsMatthew Francis
6,967 PointsThanks, one question though, why do you want to write it to a file? I'm guessing it's something relating to serialization/deserilization?