Package ca.pgon.helpers

Source Code of ca.pgon.helpers.FileHelper

/*
Copyright 2012-2013 Helpers
https://github.com/provirus/Helpers

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 ca.pgon.helpers;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import ca.pgon.helpers.exceptions.IORuntimeException;
import ca.pgon.helpers.iterable.FileLinesIterable;

/**
* Some common functions to help with files.
*
* @author Simon Levesque
*
*/
public class FileHelper {

  /**
   * Gives a destination. If the path is relative, it will be from the working
   * directory.
   *
   * @param workingDirectory
   *            the working directory if destinationPath is relative. It must
   *            end with the path separator.
   * @param destinationPath
   *            the destinationPath
   * @return the absolute path
   */
  public static String getDestination(String workingDirectory, String destinationPath) {
    String result = "";

    // Analyze
    boolean backSlash = workingDirectory.contains("\\");
    if (backSlash) {
      workingDirectory = workingDirectory.replace("\\", "/");
    }
    boolean windowsPath = isWindowsStartPath(workingDirectory);
    destinationPath = destinationPath.replace("\\", "/");

    // Absolute
    if (destinationPath.startsWith("/") || (windowsPath && isWindowsStartPath(destinationPath))) {
      result = destinationPath;
    } else {
      // Relative

      // Store root
      String root = "/";
      if (windowsPath) {
        root = workingDirectory.substring(0, 3);
      }

      // Remove the root and add the destination
      String current = workingDirectory.substring(root.length()) + destinationPath;

      // Cleanup the ..
      current = DirectoryHelper.cleanupDots(current);

      // Replace the root
      result = root + current;
    }

    // Fix the slashes
    if (backSlash) {
      result = result.replace("/", "\\");
    }

    return result;
  }

  /**
   * Tells if the path is an absolute Windows one.
   *
   * @param path
   *            the path
   * @return true if it is an absolute Windows one
   */
  public static boolean isWindowsStartPath(String path) {
    // Starts with letter, : and \
    return path.matches("^[a-zA-Z]\\:[\\\\/].*$");
  }

  /**
   * Read a file in a string.
   *
   * @param source
   *            the source file
   * @return the content
   */
  public static String readFile(File source) {
    try {
      StringBuilder sb = new StringBuilder();
      for (String line : FileHelper.readFileLinesIteration(source)) {
        sb.append(line);
        sb.append("\n");
      }

      return sb.toString();
    } catch (IORuntimeException e) {
      // Ignore since it will return null
    } catch (FileNotFoundException e) {
      // Ignore since it will return null
    }

    return null;
  }

  /**
   * Opens a file and iterates over all the lines.
   *
   * @param file
   *            the file
   * @return an iterable
   * @throws FileNotFoundException
   *             FileNotFoundException
   */
  public static FileLinesIterable readFileLinesIteration(File file) throws FileNotFoundException {
    FileLinesIterable result = new FileLinesIterable();
    result.openFile(file);
    return result;
  }

  /**
   * Opens a file and iterates over all the lines.
   *
   * @param filePath
   *            the absolute file path
   * @return an iterable
   * @throws FileNotFoundException
   *             FileNotFoundException
   */
  public static FileLinesIterable readFileLinesIteration(String filePath) throws FileNotFoundException {
    return readFileLinesIteration(new File(filePath));
  }

  public static boolean saveFile(File destination, String content) {
    try {
      OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(destination));
      write.write(content);
      write.close();
      return true;
    } catch (IOException e) {
      // Ignore since it will return false
    }

    return false;
  }
}
TOP

Related Classes of ca.pgon.helpers.FileHelper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.