public static void main(String[] args) throws IOException {
String inputFile = "PullFrom.txt";
String outputFile = "Output.txt";
Scanner input = new Scanner(new File(inputFile));
BufferedWriter out = new BufferedWriter(new FileWriter(outputFile, true));
int ID;
while (input.hasNextLine() && input.hasNextInt()) {
ID = input.nextInt();
for (int i = 0; i < 1; i++) {
try {
out.write("case "+ID+ ":");
out.newLine();
out.write("setRandomWalk(false);");
out.newLine();
out.write("break;");
out.newLine();
} catch (FileNotFoundException e) {
System.out.println("Can't Find " +inputFile );
e.printStackTrace();
} catch (IOException e) {
System.out.println("Can't create " + outputFile );
}
}
out.close();
input.close();}}}
I am trying to catch FileNotFoundException but my BufferedWriter requires that I have IOException, so I am trying to catch them both.
I am getting this in the console:
Exception in thread "main" java.io.FileNotFoundException: PullFrom.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at reader.PullFrom.main(PullFrom.java:15)
If someone could please explain to me what exactly IOException is as well that would be great, thank you!
FileNotFoundExceptionis a subclass ofIOException: it just indicates a more specific problem that has occurred whilst doing IO-related things. As such,catch (IOException e)would catch both.