try-with-resources

Try with multiple Resource in Java [duplicate]

两盒软妹~` 提交于 2019-12-03 19:28:58
问题 This question already has answers here : Close multiple resources with AutoCloseable (try-with-resources) (4 answers) Closed 6 months ago . I am new in Java8 , and I want to know if, for the AutoCloseable resource, I have to add a try for each resource , or it will work with the code above try (Connection conn = getConnection();) { Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery(sql); while (rset.next()) { TelefonicaDataVO vo = new TelefonicaDataVO(); vo

try-with-resources are not supported at this language level - Android

こ雲淡風輕ζ 提交于 2019-12-03 09:38:53
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

Why is try-with-resources catch block selectively optional?

ⅰ亾dé卋堺 提交于 2019-12-03 08:19:19
问题 I read that the catch block in try-with-resources is optional. I've tried creating a Connection object in a try-with-resources block, with no subsequent catch block, only to get compiler error from eclipse: "Unhandled exception type SQLException thrown by automatic close() invocation." Since every resource that can be used in try-with-resources implements AutoCloseable , and so potentially throws an exception upon invocation of the close() method, I don't understand how the catch clause is

Why is try-with-resources catch block selectively optional?

余生长醉 提交于 2019-12-02 22:01:37
I read that the catch block in try-with-resources is optional. I've tried creating a Connection object in a try-with-resources block, with no subsequent catch block, only to get compiler error from eclipse: "Unhandled exception type SQLException thrown by automatic close() invocation." Since every resource that can be used in try-with-resources implements AutoCloseable , and so potentially throws an exception upon invocation of the close() method, I don't understand how the catch clause is optional, given that it's not allowing me to skip catching the exception from close() . Is there some

Stream closed and not reopened - Java

女生的网名这么多〃 提交于 2019-12-02 03:42:53
I have an easy 'homework' to do, but I have found a little problem with the closure of the input stream. To put it simply, I have to make a contact 'list' application in Java, just to use the polymorphism in the correct way. So I have a class Contact and a subclass Private (contact). In both class there is a modify method to change the value of the variables. public void modify() throws IOException { System.out.println("Previously name: " + name); System.out.println("Insert new name"); try(InputStreamReader ir = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(ir) ) {

Sonar: How to use try-with-resources to close FileOutputStream

假装没事ソ 提交于 2019-12-02 00:01:20
Sonar is giving an error that this FileOutputStream should be closed. I need to modify the following code to use try-with-resources . How do I do this? public void archivingTheFile(String zipFile){ byte[] buffer = new byte[1024]; try{ FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos); for(String file : this.fileList){ ZipEntry ze= new ZipEntry(file); zos.putNextEntry(ze); FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + file); int len; while ((len = in.read(buffer)) > 0) { zos.write(buffer, 0, len); } in.close(); } zos

My own solution for Kotlin's try-with-resources absence

我怕爱的太早我们不能终老 提交于 2019-12-01 04:26:51
Kotlin provides the use function for Closeable objects, but it seems they forgot to consider AutoCloseable (e.g. DB prepared statements) for the try-with-resources full Java equivalent. I've implemented the next "home-made" solution: inline fun <T:AutoCloseable,R> trywr(closeable: T, block: (T) -> R): R { try { return block(closeable); } finally { closeable.close() } } Then you can use it the next way: fun countEvents(sc: EventSearchCriteria?): Long { return trywr(connection.prepareStatement("SELECT COUNT(*) FROM event")) { var rs = it.executeQuery() rs.next() rs.getLong(1) } } I'm new to

try-with-resources: “use” extension function in Kotlin does not always work

时光毁灭记忆、已成空白 提交于 2019-12-01 03:18:30
I had some trouble expressing the Java's try-with-resources construct in Kotlin. In my understanding, every expression that is an instance of AutoClosable should provide the use extension function. Here is a complete example: import java.io.BufferedReader; import java.io.FileReader; import org.openrdf.query.TupleQuery; import org.openrdf.query.TupleQueryResult; public class Test { static String foo(String path) throws Throwable { try (BufferedReader r = new BufferedReader(new FileReader(path))) { return ""; } } static String bar(TupleQuery query) throws Throwable { try (TupleQueryResult r =

My own solution for Kotlin's try-with-resources absence

怎甘沉沦 提交于 2019-12-01 02:15:16
问题 Kotlin provides the use function for Closeable objects, but it seems they forgot to consider AutoCloseable (e.g. DB prepared statements) for the try-with-resources full Java equivalent. I've implemented the next "home-made" solution: inline fun <T:AutoCloseable,R> trywr(closeable: T, block: (T) -> R): R { try { return block(closeable); } finally { closeable.close() } } Then you can use it the next way: fun countEvents(sc: EventSearchCriteria?): Long { return trywr(connection.prepareStatement(

try-with-resources: “use” extension function in Kotlin does not always work

荒凉一梦 提交于 2019-11-30 23:50:37
问题 I had some trouble expressing the Java's try-with-resources construct in Kotlin. In my understanding, every expression that is an instance of AutoClosable should provide the use extension function. Here is a complete example: import java.io.BufferedReader; import java.io.FileReader; import org.openrdf.query.TupleQuery; import org.openrdf.query.TupleQueryResult; public class Test { static String foo(String path) throws Throwable { try (BufferedReader r = new BufferedReader(new FileReader(path)