4

TO CLARIFY: I can't even compile due to messages from Eclipse. First code snippet: input and inputBuffer are not recognized. Second code snippet, Eclipse wants me to switch switch "Compliance and JRE to 1.7"

I am new to try-with-resources and I can't quite understand the syntax or what I'm doing wrong. Here is my code

try { FileReader input = new FileReader(this.fileName); BufferedReader inputBuffer = new BufferedReader (input); String line; while ((line = inputBuffer.readLine()) != null) { String[] inputData = line.split(","); Node<Integer> newNode = new Node<Integer>(Integer.parseInt(inputData[0]), Integer.parseInt(inputData[1])); this.hashMap.add(newNode); } //inputBuffer.close(); //input.close(); }catch (NumberFormatException nfe){ System.out.println( "Repository could not load data due to NumberFormatException: " + nfe); }catch (FileNotFoundException fnfe) { System.out.println("File not found, error: " + fnfe); }finally { inputBuffer.close(); input.close(); } 

The finally block does not work, so i wanted to try

try (FileReader input = new FileReader(this.fileName)) { ...... }catch (FileNotFoundException e) { ...... }finally { inputBuffer.close(); input.close(); } 

However

  • I should also add BufferedReader to try (...) ... but how ?

  • Also this requires me to switch "Compliance and JRE to 1.7". I don't know what that means and how that would affect my program so far, I'm not willing to do it until someone explains what it all means or if I'm doing something wrong.

EDIT

I moved declaration before try block and initialized with null, is this "ok" ?

FileReader input = null; BufferedReader inputBuffer = null; try { input = new FileReader(this.fileName); inputBuffer = new BufferedReader (input); ... } ... 
5
  • Do you have some stacktrace? Commented Dec 16, 2012 at 18:31
  • I haven't compiled yet. The problem is with a error message from eclipse. In the first example input.close() it does not recognize input because it is within a try block. And in the second example I get another system message. Commented Dec 16, 2012 at 18:32
  • 1
    docs.oracle.com/javase/tutorial/essential/exceptions/… Commented Dec 16, 2012 at 18:33
  • 1
    No matter what finally will always be executed with exception or without exception if you have try block. What error your getting and use fnfe.printStackTrace(); Commented Dec 16, 2012 at 18:34
  • 1
    If you are not comfortable with the new "try with resources", the older way is good. for more info.. checkout my answer below Commented Dec 16, 2012 at 18:42

2 Answers 2

7

You have to use Java 7 to use the try-with-resources statement.
Also the try-with-resources block uses the AutoClosable interface, so leave out those closes in your finally-block. They will be invoked automatically.
If you want to use a BufferedReader try this:

try (BufferedReader bufRead = new BufferedReader(new FileReader(this.fileName))) { ...... }catch (FileNotFoundException e) { ...... } 

You could also use multiple resources like so:

try (FileReader input = new FileReader(this.fileName); BufferedReader bufRead = new BufferedReader(input) ) { ...... }catch (FileNotFoundException e) { ...... } 

There's another important thing: If you close a BufferedReader which wraps another Reader, this underlying Reader will be closed, too.

Sign up to request clarification or add additional context in comments.

8 Comments

Ahhh, I only have java 1.6. I don't know how to update or how this would affect me. I use eclipse. Is there a solution other than try-with-resources for my current predicament ?
@Kalec use the finally block then.
@AjayGeorge If I do it says "input cannot be resolved". Same for catch block.
Did you try to declare those Readers (e.g. input) before the try-catch?
@Kalec If you have done installation of Java 7 on your machine then you can do this. Windows->Preferences->Java->Installed JRE->Exceution Environment. From here you can select your java resource
|
6

Firstly declare input and inputBuffer before try{}catch(){}

like this

FileReader input = null; BufferedReader inputBuffer = null; try { ------ ------- } 

in your first block in the finally : Check below two conditions

if(inputBuffer != null) { inputBuffer.close(); } if(input != null) { input.close(); } 

Second : if you want more than one resources in the try do this:

try ( OpenDoor door = new OpenDoor(); OpenWindow window = new OpenWindow() ) { } 

No need to close the above resources in finally block in this case.

7 Comments

Thank you. Of course it's because I declared them in the try block!
"Local variable may not have been initialized" if I try it your way (declare before try block).
sorry!! Do this: FileReader input= null; BufferedReader inputBuffer = null;
you have to initialize those variables with null. like FileReader input = null; edit: well, you were faster ;)
you have to use `<-- (the accent) before and after your keywords.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.