简体   繁体   中英

Switching project from JDK 1.7 to 1.6 BufferedReader

I had to switch my school project JDK 1.7 to 1.6. I created a new project on platform 1.6 and copied all packed in my project and seems like 1.6 doesn't support this kind of buffered reader, any help please? I need to read from a file in src. if I use Scanner should I be fine?

try (BufferedReader br = new BufferedReader(new FileReader(sDataPath))){

Error " Resource specification not allowed here for source level below 1.7" 

It's not a matter of BufferedReader being a problem - it's your try-with-resources statement , which was introduced in Java 7. You'll need to close the reader manually:

BufferedReader reader = new BufferedReader(new FileReader(sDataPath));
try {
    ...
} finally {
    reader.close();
}

As an aside, I'd advise against using FileReader - use an InputStreamReader wrapping a FileInputStream so you can specify the encoding.

Oh, and if you're allowed to use external libraries, you may find that Guava will make your resource handling simpler :)

Java 7 allows automatic disposal of resources using that try() construct. Java 6 doesn't have it. You need to write something like:

BufferedReader br=null
try {
    br = new BufferedReader(...); // create and use BufferedReader here
}
finally {
    if(br!=null) br.close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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