Resolving IOException, FileNotFoundException when using FileReader

て烟熏妆下的殇ゞ 提交于 2019-12-06 14:06:59

问题


I've not been able to resolve the following exception in the code below. What is the problem with the way I use BufferedReader? I'm using BufferedReader inside the main method

OUTPUT :-

ParseFileName.java:56: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown 

BufferedReader buffread = new BufferedReader (new FileReader("file.txt"));

// ParseFileName  is used to get the file name from a file path 
// For eg: get  - crc.v  from "$ROOT/rtl/..path/crc.v"

import java.util.regex.Pattern;
import java.io.*;

public class ParseFileName { 

  //Split along /'s , and collect the last term. 
  public String getName (String longName) { 
      String splitAt = "/";
      Pattern pattern1 = Pattern.compile(splitAt);
      String[] parts  = pattern1.split(longName);
      System.out.println("\nparts.length =  " + parts.length);

      //Return the last element in the array of strings 
      return parts[parts.length -1]; 
  }

  public static void main(String[] args) { 
    ParseFileName  superParse = new ParseFileName();  
    BufferedReader buffread = new BufferedReader (new FileReader("file.txt"));
    String line;
    while ((line = buffread.readLine())!= null) {
        String fileName = superParse.getName(line);
        System.out.println("\n" + line + "  =>  " + fileName);
    }
    buffread.close();

  }

}

UPDATE : The following works:

public static void main(String[] args) throws FileNotFoundException, IOException { 

However try.. catch still has some nagging issues for me:

try {
BufferedReader buffread = new BufferedReader (new FileReader("file.txt"));
} catch (FileNotFoundException ex) {
    ex.printStackTrace();
} catch (IOException ex2) {
   ex2.printStackTrace();
}

buffread dosent seem to get the file name. I get this error:

javac ParseFileName.java ParseFileName.java:67: cannot resolve symbol

symbol : variable buffread

location: class ParseFileName

while ((line = buffread.readLine())!= null) {

回答1:


Add throws FileNotFoundException, IOException in the header of your method. It looks like just throwing the IOException will solve your problem, but incorporating both will allow you to tell if there was a problem with the file's existence or if something else went wrong (see catch statements below).

i.e.

public static void main(String[] args) throws FileNotFoundException, IOException { 

Alternately, if you'd like to catch a specific exception and do something with it:

try {
    BufferedReader buffread = new BufferedReader (new FileReader("file.txt"));
} catch (FileNotFoundException ex) {
    // Do something with 'ex'
} catch (IOException ex2) {
    // Do something with 'ex2'
}

Update to resolve the updated issue: This is just a simple scope problem which can be solved by declaring the BufferedReader outside of the try statement.

BufferedReader buffread = null;
try {
    buffread = new BufferedReader (new FileReader("file.txt"));
} catch (FileNotFoundException ex) {
        ...



回答2:


You have to add throws statement into the signature of method main or wrap code in

try {
    ...
} catch (FileNotFoundException e) {
    ...
}



回答3:


Your code can throw FileNotFoundException or IOException which is Checked Exception. You need to surround your code in a try-catch block or add a throws declaration in your main function.




回答4:


The BufferReader can throw an exception if the file cannot be found or opened correctly.

This error message is telling you that you need to handle this exception. You can wrap the line where you create the BufferReader in a try/catch block. This will handle the case an IOException is thrown and print out the stack trace.

public static void main(String[] args) { 
    ParseFileName  superParse = new ParseFileName();  
    BufferedReader buffread;
    try
    {
        buffread= new BufferedReader (new FileReader("file.txt"));
    }
    catch(FileNotFoundException e)
    {
        e.printStackTrace();
    }

    String line;
    while ((line = buffread.readLine())!= null) {
        String fileName = superParse.getName(line);
        System.out.println("\n" + line + "  =>  " + fileName);
    }
    buffread.close();
}

Another option is to add "throws IOException" to your method header.

public static void main(String[] args) throws IOException {
    //...
}

This tells the compiler and callers of your method that you are choosing to not handle this exception and there is a chance it will be thrown.



来源:https://stackoverflow.com/questions/18968709/resolving-ioexception-filenotfoundexception-when-using-filereader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!