/*
 * Copyright 2016 kohii
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.smoothcsv.commons.utils;

import com.smoothcsv.csv.NewlineCharacter;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.stream.Collectors;

public class FileUtils {

  public static void ensureWritable(File file) {
    if (!file.exists()) {
      return;
    }
    if (!file.canWrite()) {
      file.setWritable(true);
    }
  }

  public static void ensureDirectoryExists(File dir) {
    if (!dir.exists() || !dir.isDirectory()) {
      dir.mkdirs();
    }
  }

  public static boolean canRead(File f) {
    return f.exists() && f.isFile() && f.canRead();
  }

  public static void write(List<String> content, File file, String charset) throws IOException {
    try (Writer writer =
             new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset))) {
      String lf = NewlineCharacter.DEFAULT.stringValue();
      for (int i = 0; i < content.size(); i++) {
        writer.write(content.get(i));
        writer.write(lf);
      }
      writer.flush();
    }
  }

  public static void write(String content, File file, String charset) throws IOException {
    try (Writer writer =
             new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset))) {
      writer.write(content);
      writer.flush();
    }
  }

  public static void append(List<String> content, File file, String charset) throws IOException {
    if (!file.exists()) {
      write(content, file, charset);
    } else {
      Files.write(file.toPath(), content, Charset.forName(charset), StandardOpenOption.APPEND);
    }
  }

  public static List<String> read(String filePath, String charset) throws IOException {
    return read(new File(filePath), charset);
  }

  public static List<String> read(File file, String charset) throws IOException {
    return Files.readAllLines(file.toPath(), Charset.forName(charset));
  }

  public static List<String> read(File file, String charset, final int limit) throws IOException {
    try (BufferedReader reader = Files.newBufferedReader(file.toPath(), Charset.forName("UTF-8"))) {
      return reader.lines().limit(limit).collect(Collectors.toList());
    }
  }

  public static String readAll(File file, String charset) throws IOException {
    byte[] b = Files.readAllBytes(file.toPath());
    return charset == null ? new String(b) : new String(b, charset);
  }

  public static String getCanonicalPath(File file) {
    try {
      return file.getCanonicalPath();
    } catch (IOException e) {
      return file.getAbsolutePath();
    }
  }
}

@@ -610,16 +610,76 @@
 utils;%0A%0A
+import com.smoothcsv.commons.exception.UnexpectedException;%0A
 import c
@@ -1376,16 +1376,21 @@
 %7B%0A      
+if (!
 dir.mkdi
@@ -1393,17 +1393,127 @@
 mkdirs()
-;
+) %7B%0A        throw new UnexpectedException(%22Could not make a directory. name=%22 + getCanonicalPath(dir));%0A      %7D
 %0A    %7D%0A 
@@ -3560,11 +3560,615 @@
    %7D%0A  %7D
+%0A%0A  public static File getLatestFileFromDir(File dir) %7B%0A    return getLatestFileFromDir(dir, null);%0A  %7D%0A%0A  public static File getLatestFileFromDir(File dir, File except) %7B%0A    File%5B%5D files = dir.listFiles();%0A    if (files == null %7C%7C files.length == 0) %7B%0A      return null;%0A    %7D%0A%0A    File lastModifiedFile = null;%0A    for (int i = 0; i %3C files.length; i++) %7B%0A      if (files%5Bi%5D.equals(except)) %7B%0A        continue;%0A      %7D%0A      if (lastModifiedFile == null %7C%7C lastModifiedFile.lastModified() %3C files%5Bi%5D.lastModified()) %7B%0A        lastModifiedFile = files%5Bi%5D;%0A      %7D%0A    %7D%0A    return lastModifiedFile;%0A  %7D
 %0A%7D%0A

Add auto backup on save