Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving objects in files produces error: "FileOutputStream cannot be resolved to a type"

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.

like image 981
vedran Avatar asked Nov 02 '11 11:11

vedran


1 Answers

You need to import FileOutputStream and ObjectOutputStream.

Since they are both in the java.io package, that means that you'll need to add this to the top of your file (under the package declaration, if you have one):

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

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 ObjectOutputStream and related classes.

More details about packages and importing can be found in this tutorial.

like image 192
Joachim Sauer Avatar answered Nov 09 '22 19:11

Joachim Sauer