user3447855
user3447855

Reputation: 1

try-with-resources- Not allowed for source level below 7 but i need it to work at 6

How can I modfy this to work on Java 6?

Resource specification not allowed here for source level below 1.7

The type BufferedReader is not visible


public static void findFrequency() throws IOException {
    try (BufferedReader ins = new BufferedReader(new FileReader("input.txt"))) {
        int r;
        while ((r = ins.read()) != -1) {
            text=text+String.valueOf((char)r);
            freq[r]++;
        }
    }
}

Upvotes: 0

Views: 1596

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201467

To work with earlier versions of Java (without try-with-resources), make a small change...

BufferedReader ins = null;
try { 
  ins = new BufferedReader(new FileReader("input.txt"));
  // As before...
} finally {
  if (ins != null) {
    try {
      ins.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Upvotes: 1

Related Questions