• Register
0 votes
534 views

Problem :

I want to create a simple program that will output a string to a text file. Below is my code :

import java.io.*;
public class JavaTesting {
public static void main(String[] args) {
File myfile = new File ("myfile.txt");
myfile.getParentFile().mkdirs();
PrintWriter printWriter = new PrintWriter(myfile);
printWriter.println ("Trying to write the txt using Java");
printWriter.close();       
  }
} 

But J-grasp is throwing me the following error:

 ----jGRASP exec: javac -g JavaTesting.java
JavaTesting.java:10: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
PrintWriter printWriter = new PrintWriter(myfile);
                              ^
1 error
 ----jGRASP wedge2: exit code for process is 1.

As I am very new to Java, I am unable to understand the error. Can anybody help me in resolving the error?

7.5k points

1 Answer

0 votes

Solution :

This error is occurring because you are not doing the Exception Handling and not telling compiler that there is a chance to throw a FileNotFoundException. In this situation the  FileNotFoundException will be thrown if the file does not exist.

You can refer below code:

public static void main(String[] args) throws FileNotFoundException {
File myfile = new File ("myfile.txt");
myfile.getParentFile().mkdirs();
try
{
PrintWriter printWriter = new PrintWriter(myfile);
printWriter.println ("Trying to write the txt using Java ");
printWriter.close();       
}
catch (FileNotFoundException ex)  
{
//Please insert the code to run when exception occurs
}

}

36.1k points

Related questions

1 vote
2 answers 130 views
130 views
Problem : I am very new to Java. I am trying to execute my Java code but facing below error. filecontent.java:25: unreported exception java.io.IOException; must be caught or declared to be thrown showfile(); ^ filecontent.java:88: unreported exception java.io. ... or declared to be thrown showfile(); ^ I have already thrown the java.io.IOException, however still it shows me above errors.
asked Dec 28, 2019 alecxe 7.5k points
0 votes
1 answer 353 views
353 views
Problem : I am facing a problem with the exception handling in Java, following is my code. I am facing compiler error when I try to execute my code. The error is as below: exception MojException is never thrown in body of corresponding try ... MyMojException: public class MyMojException extends Exception{ MyMojException(String str){ super(str); } } Can anyone suggest solution on my issue?
asked Dec 5, 2019 alecxe 7.5k points
1 vote
1 answer 7 views
7 views
Problem: Hello Kodlogs, I am a new Java programmer. I am practicing Java I/O file handling. I’ve written a Java program to take input a file from my computer. I’ve created a file on my desktop and reading it through my these codes: import java ... the program and it's producing an error Exception in thread "main" java.io.FileNotFoundException: file.txt Any idea to overcome this issue?
asked Jun 17 adamSw 10.9k points
0 votes
2 answers 311 views
311 views
Problem : I face the error, as “Local variable defined in an enclosing scope must be final or effectively final” and I want to know from you how to repair it the ERROR is in the menu Item-loop, where I am trying to set my textArea foreground colour to the one picked from the menuItem.
asked Jan 4 alecxe 7.5k points
0 votes
1 answer 527 views
527 views
Problem : Currently I am having issues while figuring out why am I receiving below error "Local variables referenced from an inner class must be final or effectively final". My program is running the concurrent threads to sort the array of #'s and then trying to find the high ... myhigh) myhigh = numbers[myindex]; System.out.println(myhigh); } . . . . } Above block of code is producing the error.
asked Dec 25, 2019 alecxe 7.5k points