Is it possible to use the new try-with-resources statement in Java 7 to create both the ObjectOutputStream and ObjectInputStream at the same time, and also flush the output stream before the input stream is created? 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’d like to know if there’s a better way to create and flush the streams using the new style in Java 7. Thanks!
You could nest two try-with-resources: the outer one which opens the output stream and flushes it, and then inner one which opens the input stream.