I found a method I vaguely understand on Baeldung that I repurposed for this task, but I get the gist of it. We check the string at the end of the last "." and if it equals txt then we create a new FileInputStream, otherwise we close and throw the exception. It seems straightforward. Did I miscalculate the index or something?
package com.codegym.task.task18.task1814;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Optional;
/*
UnsupportedFileName
*/
public class TxtInputStream extends FileInputStream {
public TxtInputStream(String fileName) throws IOException, UnsupportedFileNameException {
super(fileName);
if (GetExtension(fileName).equals("txt"))
new FileInputStream(fileName);
else {
super.close();
throw new UnsupportedFileNameException();
}
}
public Optional<String> GetExtension (String fileName) {
return Optional.ofNullable(fileName).filter(f -> f.contains(".")).map(f -> f.substring(fileName.lastIndexOf(".")));
}
public static void main(String[] args) {
}
}