I’m trying to save an object, I googled the HowTo and got a tutorial on this issue. However since I never worked before with this I’m experiencing problems I can’t resolve.
So I have a huge class Course (contains all sort of things, variables, collectors…) which I’m trying to save in a file.
import java.io.Serializable;
class Person implements Serializable {
... }
Now, I send an object to a class Save.java and try to save it :
class Save {
protected void saveCourse (Course course) {
FileOutputStream courseFile = new FileOutputStream("course.data");
ObjectOutputStream courseObj = new ObjectOutputStream(courseFile);
courseObj.writeObject(course);
}
}
When I try to compile it FileOutputStream and ObjectOutputStream “cannot be resolved to a type”. Aren’t they suppose to be predefined. How can I fix this
Tutorial where I got this can be found here.
You need to import
FileOutputStreamandObjectOutputStream.Since they are both in the
java.iopackage, that means that you’ll need to add this to the top of your file (under thepackagedeclaration, if you have one):Also be careful about choosing your Java tutorials: there are lots of bad tutorials out there. The official Java tutorials from Oracle are pretty good (at least they are much better than most other stuff out there) and should cover everything you need for quite some time.
For example there’s a nice tutorial about using
ObjectOutputStreamand related classes.More details about packages and importing can be found in this tutorial.