I am currently trying to complete a project in BlueJ. I have a ReadWrite class and an InvalidFileException class.
I wish to call the toString() method from the InvalidFileException class to the ReadWrite class in the writeToFile() method.
The readWrite can only handle files ending in .txt. Once the boolean completes and finds a file that does not end in .txt it should return my custom exception class.
The writeToFile method checks the boolean if the file type exists; if not, it should return the InvalidFileException which I created.
I believe I need to call it after the else statement, but I have had no luck in figuring it out.
Here is my code.
ReadWrite class:
public boolean writeToFile() {
boolean ok;
try {
FileWriter writer = new FileWriter(file);
{
if (file.toLowerCase().endsWith(".txt")) {
ok = true;
write();
} else {
ok = false;
}
}
} catch (IOException e) {
ok = false;
}
}
InvalidFileExceptionClass *****
import java.io.FileNotFoundException;
public class InvalidFileException extends Exception {
/**
* Constructor for objects of class InvalidFileException
*/
public InvalidFileException(String message) {
super(message);
}
public String toString() {
return ("Sorry but this system only accepts .txt files");
}
}