Some optimization around file writes

- Eliminate code clone
- Delete file only if file exists
- Remove unnecessary FileWriter creation
- Eliminate empty file creation step
  following append call will create one when something is written

Change-Id: Ic911dfcc2109db848ab1a0f269963191b969c8c6
diff --git a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/JavaFileGeneratorUtils.java b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/JavaFileGeneratorUtils.java
index fbbfa34..7c2959b 100644
--- a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/JavaFileGeneratorUtils.java
+++ b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/JavaFileGeneratorUtils.java
@@ -397,13 +397,7 @@
                                           ERROR_MSG_FOR_GEN_CODE);
         }
 
-        boolean isFileCreated;
         try {
-            isFileCreated = file.createNewFile();
-            if (!isFileCreated) {
-                throw new IOException("Failed to create " + file.getName() +
-                                              " class file.");
-            }
             appendContents(file, className, genType, imports, pkg);
         } catch (IOException e) {
             throw new IOException("Failed to append contents in " + file.getName() +
@@ -431,13 +425,7 @@
                                           " is already generated for: " + name + "\n" +
                                           ERROR_MSG_FOR_GEN_CODE);
         }
-        boolean isFileCreated;
         try {
-            isFileCreated = file.createNewFile();
-            if (!isFileCreated) {
-                throw new IOException("Failed to create " + file.getName() +
-                                              " class file.");
-            }
             appendContents(file, genType, imports, curNode, name);
         } catch (IOException e) {
             throw new IOException("Failed to append contents in " + file.getName() +
diff --git a/compiler/base/utils/src/main/java/org/onosproject/yang/compiler/utils/io/impl/FileSystemUtil.java b/compiler/base/utils/src/main/java/org/onosproject/yang/compiler/utils/io/impl/FileSystemUtil.java
index a4d6c10..e91af13 100644
--- a/compiler/base/utils/src/main/java/org/onosproject/yang/compiler/utils/io/impl/FileSystemUtil.java
+++ b/compiler/base/utils/src/main/java/org/onosproject/yang/compiler/utils/io/impl/FileSystemUtil.java
@@ -22,9 +22,6 @@
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintWriter;
-import java.util.ArrayList;
-import java.util.List;
-
 import static org.onosproject.yang.compiler.utils.UtilConstants.EIGHT_SPACE_INDENTATION;
 import static org.onosproject.yang.compiler.utils.UtilConstants.EMPTY_STRING;
 import static org.onosproject.yang.compiler.utils.UtilConstants.FOUR_SPACE_INDENTATION;
@@ -112,20 +109,15 @@
                                         boolean isClose)
             throws IOException {
 
-        List<FileWriter> fileWriterStore = new ArrayList<>();
 
-        FileWriter fileWriter = new FileWriter(inputFile, true);
-        fileWriterStore.add(fileWriter);
-        PrintWriter outputPrintWriter = new PrintWriter(fileWriter, true);
         if (!isClose) {
+            FileWriter fileWriter = new FileWriter(inputFile, true);
+            PrintWriter outputPrintWriter = new PrintWriter(fileWriter, true);
             outputPrintWriter.write(contentTobeAdded);
             outputPrintWriter.flush();
             outputPrintWriter.close();
         } else {
-            for (FileWriter curWriter : fileWriterStore) {
-                curWriter.flush();
-                curWriter.close();
-            }
+            // nothing to do
         }
     }
 
@@ -141,7 +133,7 @@
 
         if (file != null) {
             updateFileHandle(file, null, true);
-            if (toBeDeleted) {
+            if (toBeDeleted && file.exists()) {
                 boolean deleted = file.delete();
                 if (!deleted) {
                     throw new IOException("Failed to delete temporary file " + file.getName());
@@ -157,15 +149,7 @@
      * @throws IOException when failed to close the file handle
      */
     public static void closeFile(File file) throws IOException {
-
-        if (file != null) {
-            updateFileHandle(file, null, true);
-            boolean deleted = file.delete();
-            if (!deleted) {
-                throw new IOException("Failed to delete temporary file " +
-                                              file.getName());
-            }
-        }
+        closeFile(file, true);
     }
     // TODO follow coding guidelines in remaining of this file.
 }