user3100151
user3100151

Reputation: 69

IoException and write in file

I'm not sure where's my mistake here.

public class TestFinalClass {

public static void main(String[] args) throws IoException {
    java.io.File file = new java.io.File("Rockstar.txt");
    if (file.exists()) {
        System.out.println("file already exists");
        System.exit(1);
    }
    java.io.PrintWriter output = new java.io.PrintWriter(file);

    output.print("Bob");
    output.println("100");
    output.print("Bibelo");
    output.println("33");

    output.close();

}

}

It's simply to write some data into a file and I get that error message from eclipse when I do:

 Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
        Unhandled exception type FileNotFoundException

        at TestFinalClass.main(TestFinalClass.java:9)

Upvotes: 0

Views: 5211

Answers (3)

Aroon
Aroon

Reputation: 177

Program run perfectly: Just Give the correct url of file and make it IOException

public static void main(String[] args) throws IOException {
        java.io.File file = new java.io.File("C://Eclipse Workspace//Common_Example//NewText1.txt");
        if (file.exists()) {
            System.out.println("file already exists");
            System.exit(1);
        }
        java.io.PrintWriter output = new java.io.PrintWriter(file);

        output.print("Marc Pelletier");
        output.println("100");
        output.print("Beshario Santos");
        output.println("33");

        output.close();

    }

OUTPUT:

       Marc Pelletier100
       Beshario Santos33

Upvotes: 0

shawnfucious
shawnfucious

Reputation: 36

java.io.IoException is not a valid exception class for catching java.io.FileNotFoundException.

When compiled the following error occurs:

---------- Java Compile ----------
test.java:10: error: cannot find symbol
    public static void main(String[] args) throws IoException {
                                                  ^
  symbol:   class IoException
  location: class test
1 error

Output completed (2 sec consumed) - Normal Termination

However, if you change IoException to IOException, the code will then compile.

Also, be sure to correctly import the desired class:

import java.io.IOException;

Upvotes: 2

hineroptera
hineroptera

Reputation: 769

Take a look at an explanation of exceptions in java.

The line:

java.io.PrintWriter output = new java.io.PrintWriter(file);

potentially throws a FileNotFoundException, and you don't have any code that handles it (either by catching the exception, or having your main method declare that it throws FileNotFoundException).

This compiles for me:

import java.io.FileNotFoundException;

public class TestFinalClass {

    public static void main(String[] args) throws FileNotFoundException {
        java.io.File file = new java.io.File("Rockstar.txt");
        if (file.exists()) {
            System.out.println("file already exists");
            System.exit(1);
        }
        java.io.PrintWriter output = new java.io.PrintWriter(file);

        output.print("Marc Pelletier");
        output.println("100");
        output.print("Beshario Santos");
        output.println("33");

        output.close();

    }
}

Upvotes: 0

Related Questions