我必须将代码从1.8移到1.6,并且我得到“资源规范不允许此处的源级别低于1.7”错误。在下面你将看到部分我得到了线上机智的尝试和同时:)。我该怎么做才能解决这个问题?
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);
答案 0 :(得分:7)
尝试使用资源语句。您需要从括号中取出BufferedReader声明,如下所示:
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);
然后,为了确保关闭流(尝试使用资源语句自动为您执行此操作),您可以放置一个 finally 块来关闭流这样:
try {
(...)
} finally {
brKamera2.close();
}