I have this small code
File source;
if ( !source.exists() ) {
source = new File("instances/student"+student.getStudentID()+".data");
}
The problem is, source is not initialized. Since the whole point is to check if it exists, how do I avoid this?
Create a
Fileobject.What constructor you use depends on how you want to locate a file. A simple path String could be enough.
EDIT: just realized the source of your confusion may be that you think creating the
Fileobject will try to locate the file or create it on the file system. That’s not the case. Just callingnew File(...)won’t check its existence or try to create it. A File object is simply an abstraction for a path in your file system. It could be a directory as well.