Is there a way to create and flood I / O streams in try-with-resources?

Is it possible to use the new try-with-resources operator in Java 7 to simultaneously create both an ObjectOutputStream and an ObjectInputStream , and also clear the output stream before creating the input stream? Right now, I'm using the old style:

 ObjectOutputStream ostream = null; ObjectInputStream istream = null; try { ostream = new ObjectOutputStream(this.socket.getOutputStream()); ostream.flush(); istream = new ObjectInputStream(this.socket.getInputStream()); // ... } catch (Exception e) { e.printStackTrace(); } finally { // Close the streams. } 

I would like to know if there is a better way to create and clean up threads using the new style in Java 7. Thanks!

+4
source share
1 answer

You can nest two try-with-resources resources: an external one that opens the output stream and discards it, and then an internal one that opens the input stream.

+4
source

All Articles