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

标签 java bufferedreader

如何修改它以在 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]++;
        }
    }
}

最佳答案

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

关于java - try-with-resources- 不允许源级别低于 7,但我需要它在 6 下工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22567663/

相关文章:

java - 如何使用ListIterator?

java - StringTokenizer 无法与 BufferedReader 一起使用

java - java读取文件错误

java - 向 jList Java 添加组件

java - 将不同类型转换为一种类型

java - Spring + Quartz,触发器为空

java - 使用 ArrayLists 时如何处理深度递归?

Java - 以 'right' 方式实现排序算法

java - 在 java 中使用正则表达式时转义 '{'?

java - 正则表达式替换后格式错误的 xml