try-with-resources

Does collect operation on Stream close the stream and underlying resources?

僤鯓⒐⒋嵵緔 提交于 2019-11-27 04:33:21
问题 Does below code need to be wrapped in try-with-resources to make sure underlying file is closed? List<String> rows = Files.lines(inputFilePath).collect(Collectors.toList()); 回答1: As the javadoc of the overloaded Files#lines(Path, Charset) method states The returned stream encapsulates a Reader . If timely disposal of file system resources is required, the try-with-resources construct should be used to ensure that the stream's close method is invoked after the stream operations are completed.

Try-with-resources and return statements in java

老子叫甜甜 提交于 2019-11-27 03:02:30
问题 I'm wondering if putting a return statement inside a try-with-resources block prevents the resource to be automatically closed. try(Connection conn = ...) { return conn.createStatement().execute("..."); } If I write something like this will the Connection be closed? In the Oracle documentation it is stated that: The try-with-resources statement ensures that each resource is closed at the end of the statement. What happens if the end of the statement is never reached because of a return

Try-with-resources in Kotlin

僤鯓⒐⒋嵵緔 提交于 2019-11-27 00:39:54
问题 When I tried to write an equivalent of a Java try -with-resources code in Kotlin, it didn't work for me. I tried different variations of the following: try (writer = OutputStreamWriter(r.getOutputStream())) { // ... } But neither works. Does anyone know what should be used instead? Apparently Kotlin grammar doesn't have definition for such a construct, but maybe I'm missing something. It defines grammar for try block as follows: try : "try" block catchBlock* finallyBlock?; 回答1: There is use

Close resource quietly using try-with-resources

孤人 提交于 2019-11-26 19:59:54
问题 Is it possible to ignore the exception thrown when a resource is closed using a try-with-resources statement? Example: class MyResource implements AutoCloseable{ @Override public void close() throws Exception { throw new Exception("Could not close"); } public void read() throws Exception{ } } //this method prints an exception "Could not close" //I want to ignore it public static void test(){ try(MyResource r = new MyResource()){ r.read(); } catch (Exception e) { System.out.println("Exception:

Close multiple resources with AutoCloseable (try-with-resources)

半腔热情 提交于 2019-11-26 19:32:53
问题 I know that the resource you pass with a try, will be closed automatically if the resource has AutoCloseable implemented. So far so good. But what do I do when i have several resources that I want automatically closed. Example with sockets; try (Socket socket = new Socket()) { input = new DataInputStream(socket.getInputStream()); output = new DataOutputStream(socket.getOutputStream()); } catch (IOException e) { } So I know the socket will be closed properly, because it's passed as a parameter

Am I using the Java 7 try-with-resources correctly

一个人想着一个人 提交于 2019-11-26 18:37:16
I am expecting the buffered reader and file reader to close and the resources released if the exception is throw. public static Object[] fromFile(String filePath) throws FileNotFoundException, IOException { try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { return read(br); } } However, is there a requirement to have a catch clause for successful closure? EDIT: Essentially, is the above code in Java 7 equivalent to the below for Java 6: public static Object[] fromFile(String filePath) throws FileNotFoundException, IOException { BufferedReader br = null; try { br = new

Correct idiom for managing multiple chained resources in try-with-resources block?

做~自己de王妃 提交于 2019-11-26 15:38:53
The Java 7 try-with-resources syntax (also known as ARM block ( Automatic Resource Management )) is nice, short and straightforward when using only one AutoCloseable resource. However, I am not sure what is the correct idiom when I need to declare multiple resources that are dependent on each other, for example a FileWriter and a BufferedWriter that wraps it. Of course, this question concerns any case when some AutoCloseable resources are wrapped, not only these two specific classes. I came up with the three following alternatives: 1) The naive idiom I have seen is to declare only the top

Try With Resources vs Try-Catch

前提是你 提交于 2019-11-26 13:05:30
I have been looking at code and I have seen try with resources. I have used the standard try-catch statement before and it looks like they do the same thing. So my question is Try With Resources vs Try-Catch what are the differences between those, and which is better. Here is a try with resources : objects jar = new objects("brand"); objects can= new objects("brand"); try (FileOutputStream outStream = new FileOutputStream("people.bin")){ ObjectOutputStream stream = new ObjectOutputStream(outStream); stream.writeObject(jar); stream.writeObject(can); stream.close(); } catch(FileNotFoundException

Java 7 Automatic Resource Management JDBC (try-with-resources statement)

六月ゝ 毕业季﹏ 提交于 2019-11-26 12:07:01
问题 How to integrate the common JDBC idiom of creating/receiving a connection, querying the database and possibly processing the results with Java 7\'s automatic resource management, the try-with-resources statement? (Tutorial) Before Java 7, the usual pattern was something like this: Connection con = null; PreparedStatement prep = null; try{ con = getConnection(); prep = prep.prepareStatement(\"Update ...\"); ... con.commit(); } catch (SQLException e){ con.rollback(); throw e; } finally{ if

Try-with-resources equivalent in Java 1.6

a 夏天 提交于 2019-11-26 11:34:26
问题 I have the following code: public class Main { public static void main(String[] args) throws SQLException { try ( Connection conn = DBUtil.getConnection(DBType.HSQLDB); Statement stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery(\"SELECT * FROM tours\"); ) { DBUtil.getConnection(); } catch (SQLException e) { DBUtil.processException(e); } } } I use this code to fetch data from a database. My problem is that I\'m not