Class FileUtils

java.lang.Object
org.jppf.utils.FileUtils

public final class FileUtils extends Object
This class provides a set of utility methods for reading, writing and manipulating files.
Author:
Laurent Cohen
  • Method Details

    • readTextFile

      public static String readTextFile(Reader aReader) throws IOException
      Read the content of a specified reader into a string.
      Parameters:
      aReader - the reader to read the content from.
      Returns:
      the content of the file as a string.
      Throws:
      IOException - if the file can't be found or read.
    • textFileAsLines

      public static List<String> textFileAsLines(Reader aReader) throws IOException
      Read the content of a specified reader into a string. This method closes the reader upon terminating.
      Parameters:
      aReader - the reader to read the content from.
      Returns:
      the content of the file as a string.
      Throws:
      IOException - if the file can't be found or read.
    • readTextFile

      public static String readTextFile(File file) throws IOException
      Read the content of a specified file into a string.
      Parameters:
      file - the file to read.
      Returns:
      the content of the file as a string.
      Throws:
      IOException - if the file can't be found or read.
    • readTextFile

      public static String readTextFile(String filename) throws IOException
      Read the content of a specified file into a string.
      Parameters:
      filename - the location of the file to read.
      Returns:
      the content of the file as a string.
      Throws:
      IOException - if the file can't be found or read.
    • writeTextFile

      public static void writeTextFile(String filename, String content) throws IOException
      Write the content of a string into a specified file.
      Parameters:
      filename - the location of the file to write to.
      content - the content to write into the file.
      Throws:
      IOException - if the file can't be found or read.
    • writeTextFile

      public static void writeTextFile(File file, String content) throws IOException
      Write the content of a string into a specified file.
      Parameters:
      file - the location of the file to write to.
      content - the content to write into the file.
      Throws:
      IOException - if the file can't be found or read.
    • writeTextFile

      public static void writeTextFile(Writer dest, String content) throws IOException
      Write the content of a string into a specified file.
      Parameters:
      dest - the file to write to.
      content - the content to write into the file.
      Throws:
      IOException - if the file can't be found or read.
    • getFileInputStream

      public static InputStream getFileInputStream(File path) throws IOException
      Get an input stream given a file.
      Parameters:
      path - the file to lookup.
      Returns:
      an InputStream instance, or null if the file could not be created.
      Throws:
      IOException - if an I/O error occurs.
    • getFileInputStream

      public static InputStream getFileInputStream(String path) throws IOException
      Get an input stream given a file path. This method first looks up in the file system for the specified path, then in the classpath.
      Parameters:
      path - the path to the file to lookup.
      Returns:
      a InputStream instance, or null if the file could not be found.
      Throws:
      IOException - if an IO error occurs while looking up the file.
    • getFileOutputStream

      public static OutputStream getFileOutputStream(String path) throws IOException
      Get an output stream given a file path.
      Parameters:
      path - the path to the file to lookup.
      Returns:
      an OutputStream instance, or null if the file could not be created.
      Throws:
      IOException - if an IO error occurs while looking up the file.
    • getFileOutputStream

      public static OutputStream getFileOutputStream(File file) throws IOException
      Get an output stream given a file path.
      Parameters:
      file - the file to lookup.
      Returns:
      an OutputStream instance, or null if the file could not be created.
      Throws:
      IOException - if an IO error occurs while looking up the file.
    • getFileReader

      public static Reader getFileReader(String path) throws IOException
      Get a Reader for the specified file path, looked up first in the file system then in the classpath.
      Parameters:
      path - the path to the file to lookup.
      Returns:
      a Reader instance, or null if the file could not be found.
      Throws:
      IOException - if an IO error occurs while looking up the file.
    • getFileWriter

      public static Writer getFileWriter(String path) throws IOException
      Get a Writer for the specified file path.
      Parameters:
      path - the path to the file to create or open.
      Returns:
      a Writer instance.
      Throws:
      IOException - if an IO error occurs while creating the file writer.
    • getFilePathList

      public static List<String> getFilePathList(String fileListPath) throws IOException
      Get a list of files whose paths are found in a text file.
      Parameters:
      fileListPath - the path to the file that holds the list of documents to validate.
      Returns:
      the file paths as a lst of strings.
      Throws:
      IOException - if an error occurs while looking up or reading the file.
    • getFileExtension

      public static String getFileExtension(File file)
      Get the extension of a file.
      Parameters:
      file - the file from which to get the extension.
      Returns:
      the file extension, or null if it si not a file or does not have an extension.
    • getFileName

      public static String getFileName(String filePath)
      Get the name of a file from its full path.
      Parameters:
      filePath - the file from which to get the file name.
      Returns:
      the file name without path information.
    • splitTextFile

      public static void splitTextFile(String file, int splitSize) throws IOException
      Split a file into multiple files whose size is as close as possible to the specified split size.
      Parameters:
      file - the text file to split.
      splitSize - the maximum number of lines of each resulting file.
      Throws:
      IOException - if an IO error occurs.
    • getPathAsByte

      public static byte[] getPathAsByte(String path) throws IOException
      Get the content of a file or resource on the classpath as an array of bytes.
      Parameters:
      path - the path of the file to read from as a string.
      Returns:
      a byte array with the file content.
      Throws:
      IOException - if an IO error occurs.
    • getFileAsByte

      public static byte[] getFileAsByte(File file) throws IOException
      Get the content of a file as an array of bytes.
      Parameters:
      file - the abstract path of the file to read from.
      Returns:
      a byte array with the file content.
      Throws:
      IOException - if an IO error occurs.
    • toFiles

      public static File[] toFiles(File dir, String... names)
      Convert a set of file names into a set of File objects.
      Parameters:
      dir - the directory in which the files are located
      names - the name part of each file (not the full path)
      Returns:
      an array of File objects.
    • toURLs

      public static URL[] toURLs(File... files)
      Convert a set of file paths into a set of URLs.
      Parameters:
      files - the files whose path is to be converted to a URL.
      Returns:
      an array of URL objects.
    • writeBytesToFile

      public static void writeBytesToFile(byte[] data, File path) throws IOException
      Write a byte array into an file.
      Parameters:
      data - the byte array to write.
      path - the path to the file to write to.
      Throws:
      IOException - if an I/O error occurs.
    • deletePath

      public static boolean deletePath(File path)
      Delete the specified path, recursively if this is a directory.
      Parameters:
      path - the path to delete.
      Returns:
      true if the folder and all contained files and subfolders were deleted, false otherwise.
    • deletePath

      public static boolean deletePath(File path, boolean childrenOnly)
      Delete the specified path, recursively if this is a directory.
      Parameters:
      path - the path to delete.
      childrenOnly - when true only delete the children of the specified path, if it is a directory.
      Returns:
      true if the folder and all contained files and subfolders were deleted, false otherwise.
    • mkdirs

      public static void mkdirs(File file) throws IOException
      Create the folders of the specified path, if they do not all already esist.
      Parameters:
      file - the path for which to create the folders. If it is a file, then folders for its parent path are created.
      Throws:
      IOException - if the folders could not be created.
    • getURLFromFilePath

      public static URL getURLFromFilePath(String path)
      Transform a file path into a URL.
      Parameters:
      path - the path to transform.
      Returns:
      the path expressed as a URL.
    • getURLFromFilePath

      public static URL getURLFromFilePath(File path)
      Transform a file path into a URL.
      Parameters:
      path - the path to transform.
      Returns:
      the path expressed as a URL.
    • getDecodedURLFromFilePath

      public static String getDecodedURLFromFilePath(File path)
      Transform a file path into a URL in non URL-ncoded form.
      Parameters:
      path - the path to transform.
      Returns:
      the a string representing the decoded URL.
    • getJPPFTempDir

      public static File getJPPFTempDir()
      Returns:
      the root folder for JPPF temporary files.
    • getFileNameAndExtension

      public static Pair<String,String> getFileNameAndExtension(String imageName)
      Decompose a file name into name + extension.
      Parameters:
      imageName - the file name to convert.
      Returns:
      a Pair of name, extension.
    • getBufferedReader

      public static BufferedReader getBufferedReader(String path) throws IOException
      Get a Reader for the specified file path, looked up first in the file system then in the classpath.
      Parameters:
      path - the path to the file to lookup.
      Returns:
      a Reader instance, or null if the file could not be found.
      Throws:
      IOException - if an IO error occurs while looking up the file.
    • getFirstExistingFilePath

      public static String getFirstExistingFilePath(String... paths)
      Get the first path in the provided array which exists in the file system.
      Parameters:
      paths - the array pf paths to check.
      Returns:
      the first existing path, or null if none exists.