This is a new syntax present since Java 7, it's called try
-with-resources. It's a shortcut to ensure that the resource opened within the try
is closed automatically when the block ends, what you'd normally do in a finally
block. Quoting the tutorial, this:
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.readLine();
}
… is equivalent to this:
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
if (br != null)
br.close();
}
Now regarding the code in the question: it's creating a new file only if it didn't exist before (see this answer), and it's opening an ObjectOutPutStream
for writing to it.