public class TxtInputStream extends FileInputStream {
private final String fileName;
public TxtInputStream(String fileName) throws IOException, UnsupportedFileNameException {
this.fileName = fileName;
if(fileName.substring(fileName.length() -4).equals(".txt"))
new FileInputStream(fileName);
else{
throw new UnsupportedFileNameException();
super.close();
}
}
public static void main(String[] args) {
}
}
i need a if/else in constructor but super(name) has to be the first line in constructor.
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Ștefan Marinescu
8 April 2019, 17:47
First you initiate the super constructor and after that you check if the file name ends with .txt. You can do that by calling the method endsWith from the String class.If it doesn't then you have to call the close() method from the superclass and throw the custom Exception.
+1
Djole
9 April 2019, 10:08
Thanks!
0