package eg.utils;

import java.io.File;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Static methods to work with files
 */
public class FileUtils {
   
   public static String fileSuffix(String file) {
      int indDot = file.lastIndexOf(".") + 1;
      int indFileSep = file.lastIndexOf(File.separator);
      if (indDot > indFileSep) {
         return file.substring(indDot);
      }
      else {
         return "";
      }
   }
   
   /**
    * Deletes a folder and its content
    *
    * @param dir  the directory to be deleted
    * @return  true if the directory has been deleted
    */
   public static boolean deleteFolder(File dir) {
      boolean ret = true;
      if (dir.isDirectory()){
         for (File f : dir.listFiles()) {
            ret = ret && FileUtils.deleteFolder(f);
         }
      }
      return ret && dir.delete();
   }
   
   /**
    * If the specified directory is empty
    *
    * @param dir  the directory
    * @return  if dir is empty
    */
   public static boolean isFolderEmpty(File dir) {
      File[] content = dir.listFiles();
      return content.length == 0;
   }
   
   /**
    * Appends to the file 'log.txt' the stack trace of an exception
    * and shows a warning in a dialog window.
    * <p>The "log" file is saved in the program's directory.
    *
    * @param e  an <code>Exception</code>
    */
   public static void logStack(Exception e) { 
      File logFile = new File("log.txt");
      try (PrintWriter pw = new PrintWriter(new FileOutputStream(logFile, true))) {
         e.printStackTrace(pw);
         Dialogs.errorMessage("Error:\n" + e.getMessage()
               + ".\nSee log.txt file");
      }
      catch(IOException ioe) {
         throw new RuntimeException(
               "Could not write the stack trace to the log file", ioe);
      }
   }
   
   /**
    * Empties the 'log' file saved in the program's directory
    */
   public static void emptyLog() { 
      try (FileWriter writer = new FileWriter("log.txt", false)) {
         writer.write("");
      }
      catch(IOException ioe) {
          throw new RuntimeException("Could not empty the log file", ioe);
      }
   }
}

@@ -52,107 +52,83 @@
 .io.
-PrintWriter;%0Aimport java.io.FileWriter;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException
+FileWriter;%0Aimport java.io.IOException;%0A%0A//--Eadgyth--/%0Aimport eg.Constants
 ;%0A%0A/
@@ -1302,13 +1302,11 @@
 The 
-%22
 log
-%22
  fil
@@ -1366,18 +1366,19 @@
 aram e  
-an
+the
  %3Ccode%3EE
@@ -1445,17 +1445,16 @@
 ion e) %7B
- 
 %0A      F
@@ -1457,23 +1457,17 @@
    File 
-logFile
+f
  = new F
@@ -1497,110 +1497,294 @@
 ry (
-Print
+File
 Writer 
-p
 w
+riter
  = new 
-Print
+File
 Writer(
-new FileOutputStream(logFile, true))) %7B%0A         e.printStackTrace(pw
+f, true)) %7B%0A         writer.write(e.getMessage() + Constants.LINE_SEP);%0A         for (StackTraceElement el : e.getStackTrace()) %7B%0A            writer.write(%22   %22 + el.toString() + Constants.LINE_SEP);%0A         %7D%0A         writer.write(Constants.LINE_SEP
 );%0A 
@@ -1812,16 +1812,32 @@
 Message(
+%0A               
 %22Error:%5C
@@ -1855,31 +1855,16 @@
 essage()
-%0A              
  + %22.%5CnS
@@ -2281,32 +2281,29 @@
 IOException 
-io
 e) %7B%0A
- 
          thr
@@ -2350,34 +2350,32 @@
  the log file%22, 
-io
 e);%0A      %7D%0A   %7D

write error log better formatted