best practices with Java try with resources

36 views Asked by At

Is there any difference between the two below code snippets, in terms of GC, resource closing etc?

or both are same/correct technically?

#Option 1
try(Writer writer = new OutputStreamWriter(new FileOutputStream(new File(path)), StandardCharsets.UTF_8)) {

}

#Option 2
try(FileOutputStream fis = new FileOutputStream(new File(path);
    Writer writer = new OutputStreamWriter(fis, StandardCharsets.UTF_8)) {

}

Just to understand what's the best approach to use in a highly used java application.

0

There are 0 answers