Make use of try with resource 53/44853/1
authorKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Thu, 26 Apr 2018 10:19:02 +0000 (15:49 +0530)
committerKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Thu, 26 Apr 2018 10:19:02 +0000 (15:49 +0530)
Issue-ID: CLI-100

Change-Id: I96a0e3cf0e982c001adf6d6bf3c0a5ad8c23a110
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java

index 0c0c5a1..1f51ff3 100644 (file)
@@ -220,11 +220,10 @@ public class OnapCommandPrint {
      *             exception
      */
     public String printCsv() throws OnapCommandOutputPrintingFailed {
-        StringWriter writer = new StringWriter();
-        CSVPrinter printer = null;
-        try {
-            CSVFormat formattor = CSVFormat.DEFAULT.withRecordSeparator(System.getProperty("line.separator"));
-            printer = new CSVPrinter(writer, formattor);
+        CSVFormat formattor = CSVFormat.DEFAULT.withRecordSeparator(System.getProperty("line.separator"));
+
+        try (StringWriter writer = new StringWriter();
+             CSVPrinter printer = new CSVPrinter(writer, formattor);) {
 
             List<List<Object>> rows = this.formRows(false);
 
@@ -235,15 +234,6 @@ public class OnapCommandPrint {
             return writer.toString();
         } catch (IOException e) {
             throw new OnapCommandOutputPrintingFailed(e);
-        } finally {
-            try {
-                if (printer != null) {
-                    printer.close();
-                }
-                writer.close();
-            } catch (IOException e) {
-                throw new OnapCommandOutputPrintingFailed(e);  // NOSONAR
-            }
         }
     }