In this task, is it generally possible to avoid creating two objects of the FileInputStream class as in my code?
public class TxtInputStream extends FileInputStream {
public TxtInputStream(String fileName) throws Exception {
super(fileName);
if (fileName.endsWith(".txt")) {
new FileInputStream(fileName);
} else {
super.close();
throw new UnsupportedFileNameException();
}
}
2 or 1?
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Ahmed
24 August 2019, 09:14solution
You don't need to create another object in the if clause. You can delete the else clause totally and put it's content to the if clause. Don't forget to put not in the if clause then -->
+5
Alexander Shtunder
24 August 2019, 12:57
Your answer will really work too. Now I understand how to avoid creating two objects. Thank you.
+1