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) {
2
  • Are you getting a compilation error or exception? It should give you compile error. Commented Sep 23, 2013 at 20:59
  • This has exactly nothing to do with BufferedReader, and not much to do with FileReader either really. Commented Feb 4, 2018 at 2:26

4 Answers 4

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) {
        ...
Sign up to request clarification or add additional context in comments.

5 Comments

Still his code won't compile because line = buffread.readLine() will throw IOException
Thanks, I resolved the exception by using public static void main(String[] args) throws FileNotFoundException, IOException { , but I want to use the try .. catch syntax instead. I'll try that
@Nik No problem. Make sure to mark this as the answer if this is the route you're taking.
@ElliotSchmelliot I still see a (silly?) error using try.. catch block. Do you know what that could be?
@Nik Yes I see. This is just a simple scope issue. See my update.
1

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

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

Comments

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.

Comments

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.

1 Comment

FileReader throws the exception. BufferedReader hasn't even executed at that point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.