try-with-resources

Java implicit try-with-resources

六月ゝ 毕业季﹏ 提交于 2019-12-22 01:45:57
问题 I am wondering if the following code uses the try-with-resources correctly. try (ResultSet rs = new QueryBuilder(connection, tableName(), getPaths(), searchQuery()).add(constraint).build().executeQuery()) { while (rs.next()) { beans.add(createBean(rs)); } } The arguments are not important, the only important thing is: new QueryBuilder().build(); returns a PreparedStatement . I completely understand that rs will be closed, but will the PreparedStatement also be closed, and if so, for what

Try With Resources Not Supported at This Language Level

*爱你&永不变心* 提交于 2019-12-21 11:37:11
问题 I'm using IntelliJ IDEA Ultimate 2016.2.1, have set Project SDK to my 1.8 version, Project Language Level to 8, Module SDK to my 1.8 version, and JDK home path to /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home. I have restarted the IDE. Still, I am getting ugly warnings about try-with-resources not supported at this language level. I have not used less than language level 8 in IntelliJ so this can't be due to something not refreshing. I am using a gradle build configuration -

Try With Resources Not Supported at This Language Level

十年热恋 提交于 2019-12-21 11:36:24
问题 I'm using IntelliJ IDEA Ultimate 2016.2.1, have set Project SDK to my 1.8 version, Project Language Level to 8, Module SDK to my 1.8 version, and JDK home path to /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home. I have restarted the IDE. Still, I am getting ugly warnings about try-with-resources not supported at this language level. I have not used less than language level 8 in IntelliJ so this can't be due to something not refreshing. I am using a gradle build configuration -

In Java, how to check that AutoCloseable.close() has been called?

五迷三道 提交于 2019-12-20 09:48:47
问题 I am authoring a java library. Some of the classes that are meant to be used by library users, hold native system resources (over JNI). I'd like to ensure that the user "disposes" these objects, as they are heavy, and in a testsuite they may cause leakage between testcases (for example, I need to ensure TearDown will dispose). For this purpose I made the Java classes implement AutoCloseable, but this doesn't seem to suffice, or I'm not using it correctly: I don't see how to use try-with

Stream closed and not reopened - Java

你说的曾经没有我的故事 提交于 2019-12-20 03:56:32
问题 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

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

廉价感情. 提交于 2019-12-20 02:44:09
问题 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

What is the best way to emulate try-with-resources in Java 6?

本小妞迷上赌 提交于 2019-12-19 17:04:09
问题 It turns out that almost nobody closes resources in Java correctly. Programmers either do not use try-finally block at all, or just put resource.close() in finally which is also incorrect (because Throwable from close() can shadow Throwable from try block). Sometimes they put something like IOUtils.closeQuietly() with is only correct for InputStream , but not for OutputStream . try-with-resources solves all of these problems but there are still huge number of projects written in Java 6. What

Is flush() call necessary when using try-with-resources

倖福魔咒の 提交于 2019-12-18 12:49:47
问题 Will try-with-resources call flush() implicitly? If it does, in the following code snippet, bw.flush() can be safely removed? static void printToFile1(String text, File file) { try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) { bw.write(text); bw.flush(); } catch (IOException ex) { // handle ex } } ps. I don't see any description about it in official document: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html https://docs.oracle.com/javase/8

Try / Try-with-resources and Connection, Statement and ResultSet closing

半城伤御伤魂 提交于 2019-12-18 03:58:17
问题 I have recently having some discussions with my professor about how to handle the basic jdbc connection scheme. Suppose we want to execute two queries, this is what he proposes public void doQueries() throws MyException{ Connection con = null; try { con = DriverManager.getConnection(dataSource); PreparedStatement s1 = con.prepareStatement(updateSqlQuery); PreparedStatement s2 = con.prepareStatement(selectSqlQuery); // Set the parameters of the PreparedStatements and maybe do other things s1

What is a suppressed exception?

耗尽温柔 提交于 2019-12-17 05:46:08
问题 A comment (by user soc) on an answer to a question about tail call optimisation mentioned that Java 7 has a new feature called "suppressed exceptions", because of "the addition of ARM" (support for ARM CPUs?). What is a "suppressed exception" in this context? In other contexts a "suppressed exception" would be an exception that was caught and then ignored (rarely a good idea); this is clearly something different. 回答1: I believe the commenter is referring to is an exception which is semi