此语言级别不支持try-with-resources-Android [英] try-with-resources are not supported at this language level - Android

查看:334
本文介绍了此语言级别不支持try-with-resources-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下发布的代码中,我在android中存在此语言级别不支持try-with-resources"的问题,我尝试将语言设置为7,但它仍然会给我相同的示例,并且会一直给我我可以选择更改为语言7.

I have a problem with "try-with-resources are not supported at this language level" in android in the following posted code, I tried to set language to 7 but it stills keeps giving me the same example plus it keeps giving me the option to change to language 7.

public String ReadFile(String fileName) {

    try (BufferedReader br = new BufferedReader(new FileReader(fileName+".txt"))) {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append(System.lineSeparator());
            line = br.readLine();
        }

        String everything = sb.toString();
        return everything;
    } catch (FileNotFoundException ex) {
        Logger.getLogger(SaveNLoadRank.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(SaveNLoadRank.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "1";
}

仅当minSdkVersion设置为19或更高时,才支持

推荐答案

尝试资源.

由于我怀疑您的应用程序支持的最低API版本为19或更高(2014年6月),因此很可能是您遇到的问题.

Since I doubt your application support a minimum API version of 19 or higher (in June of 2014), that is likely your problem.

在2014年3月发布的SDK Tools Revision 22.6中添加了对Java 7语言功能的支持(

Support for Java 7 language features was added in SDK Tools Revision 22.6 released in March of 2014 (see here). However, try-with-resources is not a feature that is possible to introduce for previous versions of Android, so applications using that feature must run on 19+, thus the minSdkVersion requirement.

更新 现在,您可以将try-with-resources与任何API结合使用.

UPDATE You can now use try-with-resources with any API.

除了上述Java 8语言功能和API,Android Studio 3.0及更高版本将对尝试资源的支持扩展到所有 Android API级别.

In addition to the Java 8 language features and APIs above, Android Studio 3.0 and later extends support for try-with-resources to all Android API levels.

要开始使用受支持的Java 8语言功能,请将Android插件更新为3.0.0(或更高版本).之后,对于每个使用Java 8语言功能的模块(无论是通过其源代码还是通过依赖项),在Project Structure对话框中将Source Compatibility和Target Compatibility都更新为1.8,如图2所示(单击File> Project Structure).

To start using supported Java 8 language features, update the Android plugin to 3.0.0 (or higher). After that, for each module that uses Java 8 language features (either in its source code or through dependencies), update the Source Compatibility and Target Compatibility to 1.8 in the Project Structure dialog as shown in figure 2 (click File > Project Structure).

https://developer.android.com/studio/write/java8- support.html

这篇关于此语言级别不支持try-with-resources-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆