Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "Resource specification not allowed here for source level below 1.7"?

Tags:

java

I had to move my code to 1.6 from 1.8, and i get "Resource specification not allowed here for source level below 1.7" error. below u will see part wher i get the eror at line wit Try and while:). What can i do to fix this?

StringBuilder resultKamera2 = new StringBuilder();

                {
                try (BufferedReader brKamera2 = new BufferedReader(new FileReader("D:/test1.txt"))) {
                while ((lineKamera2 = brKamera2.readLine()) != null) {

                Matcher categoryMatcherKamera2 = CategorieKamera2.matcher(lineKamera2);                    
                Matcher itemMatcherKamera2 = CategorieSiCantitateKamera2.matcher(lineKamera2);
like image 435
enache vladd Avatar asked Nov 02 '14 19:11

enache vladd


1 Answers

try with resources statement was introduced in Java SE 7. You need to take the BufferedReader declaration out of the parentheses like this:

StringBuilder resultKamera2 = new StringBuilder();

            {
            try  {
                BufferedReader brKamera2 = new BufferedReader(new FileReader("D:/test1.txt")
                while ((lineKamera2 = brKamera2.readLine()) != null) {

                Matcher categoryMatcherKamera2 = CategorieKamera2.matcher(lineKamera2);                   
                Matcher itemMatcherKamera2 = CategorieSiCantitateKamera2.matcher(lineKamera2);

And then, to ensure that the stream will be closed (try with resources statement does that automatically for you) you can put a finally block to close the stream like this:

try { 
    (...)
} finally {
    brKamera2.close();
}
like image 190
FruitAddict Avatar answered Nov 15 '22 01:11

FruitAddict