Вопрос

I was practicing reading text from a file and could not understand what I was doing wrong. This is what I had at first:

public class Main {
public static void main(String[] args){
File f = new File("C:\\test\\est.txt");
            Scanner in = new Scanner(f);
            while (in.hasNext()){
                System.out.println(in.next());
            }
      }
}

The compiler said: Unhandled exception java.io.fileNotFoundException. So I tried this:

File f = new File("C:\\test\\est.txt");
    Scanner in = new Scanner(f);
    try{
        while (in.hasNext()){
            System.out.println(in.next());
        }
    }catch (IOException i){
        System.out.println(i.getMessage());
    }

Now the compiler said: Unhandled exception java.io.fileNotFoundException. And also: java.io.fileNotFoundException is never thrown in the corresponding try block.

Then I tried this:

File f = new File("C:\\test\\est.txt");
    Scanner in = new Scanner(f);
    try{
        while (in.hasNext()){
            System.out.println(in.next());
        }
    }catch (IOException i){
        throw new IOException(i.getMessage());
    }

However it still says: Unhandled exception java.io.fileNotFoundException!

Can anyone please explain what I am doing wrong & how I can read all the text from a file in a manner that looks similar to my attempt.

**Note my file DOES exist.

Это было полезно?

Решение

An unhandled exception means that it might be thrown somewhere in your code, but you are not taking it into account when you explicitly have to. So to fix, wrap it in a try-catch:

File f = new File("C:\\test\\est.txt");
try
{
    Scanner in = new Scanner(f);
    while (in.hasNext()){
        System.out.println(in.next());
    }
}catch (IOException i){
    e.printStackTrace();
}

Note that the Scanner is also inside the try block. Your attempt was good, but the Scanner constructor also might throw a FileNotFoundException. To fix such issues in the future, the compiler tells you exactly which line that throws the exception, not being handled.

Другие советы

Well the error tells you what the problem is ;-) You are handling the IOException already, but not the FileNotFoundException!

Try like this:

File f = new File("C:\\test\\est.txt");
Scanner in = null;
try {
   in = new Scanner(f);
} catch (IOException i){
   i.printStackTrace();
}

while (in != null && in.hasNext()){
   System.out.println(in.next());
}

edit: Ok actually FileNotFoundException extends IOException, so you don't need to handle it seperately obviously :)

You need to handle the FileNotFoundException by either saying:

throws FileNotFoundException

in your method header. Or adding a catch statement:

}catch (FileNotFoundException ex){
    //log it or handle it
}

Also, avoiding throwing the same exception in the catch:

throw new IOException(i.getMessage());

So you want something like:

try{
    File f = new File("C:\\test\\est.txt");
    Scanner in = new Scanner(f);
    while (in.hasNext()){
        System.out.println(in.next());
    }
}catch (FileNotFoundException ex){
    //log it or handle it
}catch (IOException i){
    //throw new IOException(i.getMessage());
    //log it or handle it 
}

Ok, but now the compiler prints java.io.FileNotFoundException: C:\Users\Cristian\Desktop (Access is denied). Why is "Access denied?"

Make sure you have read access of that directory. Try running your IDE as administrator (you can do this by right-clicking on the IDE and click Run As Administrator).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top