Use try-with-resource to close filestream 83/18483/1
authorsurya-huawei <a.u.surya@huawei.com>
Thu, 12 Oct 2017 08:56:36 +0000 (14:26 +0530)
committersurya-huawei <a.u.surya@huawei.com>
Thu, 12 Oct 2017 08:58:05 +0000 (14:28 +0530)
*Using try-with-resource here makes sure that the resource is
automatically closed and increases code readability

Issue-Id: CCSDK-117
Change-Id: Ie923e4540d5be90324104593a1d5fcb1bd501f5c
Signed-off-by: surya-huawei <a.u.surya@huawei.com>
vnftools/provider/src/main/java/org/onap/sdnc/vnftools/VnfTools.java

index fb95a41..08ed435 100644 (file)
@@ -162,18 +162,13 @@ public class VnfTools implements SvcLogicJavaPlugin {
                }
 
                PrintStream pstr = null;
-               FileOutputStream fileStream = null;
 
-               try {
-                       pstr = new PrintStream(fileStream = new FileOutputStream(new File(fileName), true));
-               } catch (Exception e) {
-                       if (fileStream != null) {
-                               try {
-                                       fileStream.close();
-                               } catch (IOException e1) {
-                                       LOG.error("FileOutputStream close exception: ", e1);
-                               }
-                       }
+               try (FileOutputStream fileStream = new FileOutputStream(new File(fileName), true)){
+                       pstr = new PrintStream(fileStream);
+               } catch (IOException e1) {
+                       LOG.error("FileOutputStream close exception: ", e1);
+               }
+               catch (Exception e) {
                        throw new SvcLogicException("Cannot open file " + fileName, e);
                }