简体   繁体   English

使用Try-with-Resources时的编译错误

[英]Error in compilation when using Try-with-Resources

I am a newbie on Java and writing a code of Try-with-Resources. 我是Java的新手,正在编写Try-with-Resources代码。 I am using JRE_1.7.0_51. 我正在使用JRE_1.7.0_51。 The code is... 代码是...

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class app2 {
    public static void main(String[] args) {
        File file=new File("test.txt");
        FileReader fr=new FileReader(file);

        try(BufferedReader br=new BufferedReader(fr)){

        }

     }
}

However post execution I get the following eror-- 但是执行后我得到以下错误-

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Syntax error on token "(", { expected
    Syntax error on token ")", delete this token
    Syntax error, insert ";" to complete LocalVariableDeclarationStatement
    Syntax error, insert "}" to complete Block

    at App.main(App.java:13)

Not sure what I am doing wrong. 不知道我在做什么错。 Need help. 需要帮忙。 Thanks in advance. 提前致谢。

What compiler are you using? 您正在使用什么编译器? The only thing missing is a check for IOException . 唯一缺少的是对IOException的检查。 The following code compiles without problems 以下代码编译没有问题

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class app2 {
    public static void main(String[] args) throws Exception {
        File file=new File("test.txt");
        FileReader fr=new FileReader(file);

        try(BufferedReader br=new BufferedReader(fr)){

        }

     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM