尝试资源-不允许源代码级别低于7,但我需要它在6

2022-07-19 00:00:00 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]++;
        }
    }
}

解决方案

若要使用早期版本的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();
    }
  }
}

相关文章