1

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) {
user207421
  • 305,947
  • 44
  • 307
  • 483
Nikhil
  • 797
  • 6
  • 12
  • 30

4 Answers4

7

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) {
        ...
ElliotSchmelliot
  • 7,322
  • 4
  • 41
  • 64
1

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

try {
    ...
} catch (FileNotFoundException e) {
    ...
}
briarheart
  • 1,906
  • 2
  • 19
  • 33
1

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.

Community
  • 1
  • 1
Rafi Kamal
  • 4,522
  • 8
  • 36
  • 50
0

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.

Hampton Terry
  • 344
  • 1
  • 13