I am writing a simple program, it writes “I love Java” 300 times to a text file, and I have it working as shown.
public class PrintLines {
public static void main(String[] args) throws Exception{
java.io.File file = new java.io.File("myFile.txt");
java.io.PrintWriter output = new java.io.PrintWriter(file);
int count = 1;
while (count <= 300) {
output.println(count + " I love Java!");
count++;
}
output.close();
}
}
What I am unsure about, and have been trying to figure out on several webpages and the text book I use for class, is how do I do a try/catch block for this code and still create the file? It works, as I said, but I have to do it without the throws Exception in there. Can’t find a decent webpage which gives newbie answers for beginning programmers like me.
Your code should look similar to this.
The EXCEPTION should be IOException, I think.