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";
}
It is not supported below API 19, but it reportedly works on 15 and maybe even 14: https://code.google.com/p/android/issues/detail?id=73483
try-with-resources is only supported if your
minSdkVersion
is set to 19 or higher.Since I doubt your application support a minimum API version of 19 or higher (in June of 2014), that is likely your problem.
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.UPDATE You can now use try-with-resources with any API.
https://developer.android.com/studio/write/java8-support.html