FileInput Help/Advice

Posted by user559142 on Stack Overflow See other posts from Stack Overflow or by user559142
Published on 2011-01-09T18:44:18Z Indexed on 2011/01/09 18:53 UTC
Read the original article Hit count: 144

Filed under:
|
|
|

I have a fileinput class. It has a string parameter in the constructor to load the filename supplied. However it just exits if the file doesn't exist. I would like it to output a message if the file doesn't exist - but not sure how....

Here is the class:

public class FileInput extends Input {

/**
 * Construct <code>FileInput</code> object given a file name.
 */
public FileInput(final String fileName) {
    try {
        scanner = new Scanner(new FileInputStream(fileName));
    } catch (FileNotFoundException e) {
        System.err.println("File " + fileName + " could not be found.");
        System.exit(1);
    }

}

/**
 * Construct <code>FileInput</code> object given a file name.
 */
public FileInput(final FileInputStream fileStream) {
    super(fileStream);
}

}

And its implementation:

private void loadFamilyTree() {
    out.print("Enter file name: ");
    String fileName = in.nextLine();
    FileInput input = new FileInput(fileName);
    family.load(input);
    input.close();
}

© Stack Overflow or respective owner

Related posts about java

Related posts about file