try-with-resources-不允许源级别低于7,但我需要它在6下工作

时间:2014-03-21 19:17:13

标签: java bufferedreader

如何修改它以适用于Java 6?

  

此处不允许源级别低于1.7的资源规范

     

BufferedReader类型不可见


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]++;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

要使用早期版本的Java(没有try-with-resources),请进行一些小改动......

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();
    }
  }
}