FilenameReader.java

package edu.odu.cs.cs350;

import java.io.File;
import java.io.FileNotFoundException;
/**
 * <p>
 * This class reads the file path or file name and makes sure that the specified file exists.
 * If it exists it returns the file name to the main class. If it does not exist it throws a 
 * {@link FileNotFoundException}.
 * </p>
 */
public class FilenameReader {
    /**
     * Reads the file, validates that
     * the referenced file exists, and returns the filename as a string.
     * <p>
     * If the user enters a filename that does not correspond to an existing file
     * on the filesystem, this method throws a {@link FileNotFoundException}.
     * </p>
     *
     * @return the validated filename
     * @param filePath the path to the file
     * @throws FileNotFoundException if the file does not exist or is not a regular file
     */
    public static String getFilename(String filePath) throws FileNotFoundException {
        File file = new File(filePath);
        if (!file.exists() || !file.isFile()) {
            throw new FileNotFoundException("File not found: " + filePath);
        }
        return file.getName();
    }
}