Package de.jplag.util

Class FileUtils

java.lang.Object
de.jplag.util.FileUtils

public class FileUtils extends Object
Encapsulates various interactions with files to prevent issues with file encodings.
  • Method Details

    • openFileReader

      public static BufferedReader openFileReader(File file) throws IOException
      Opens a file reader, guessing the charset from the content. Also, if the file is encoded in a UTF* encoding and a bom exists, it is removed from the reader.
      Parameters:
      file - The file to open for read
      Returns:
      The reader, configured with the best matching charset
      Throws:
      IOException - If the file does not exist for is not readable
    • readFileContent

      public static String readFileContent(File file) throws IOException
      Reads the contents of a file into a single string.
      Parameters:
      file - The file to read
      Returns:
      The files content as a string
      Throws:
      IOException - If an IO error occurs
      See Also:
    • detectCharset

      public static Charset detectCharset(File file) throws IOException
      Detects the charset of a file. Prefer using openFileReader(File) or readFileContent(File) if you are only interested in the content.
      Parameters:
      file - The file to detect
      Returns:
      The most probable charset
      Throws:
      IOException - If an IO error occurs
    • detectCharsetFromMultiple

      public static Charset detectCharsetFromMultiple(Collection<File> files) throws ParsingException
      Detects the most probable charset over the whole set of files.
      Parameters:
      files - The files to check
      Returns:
      The most probable charset
      Throws:
      ParsingException
    • openFileWriter

      public static Writer openFileWriter(File file) throws IOException
      Opens a file writer, using the default charset for JPlag
      Parameters:
      file - The file to write
      Returns:
      The file writer, configured with the default charset
      Throws:
      IOException - If the file does not exist or is not writable
    • write

      public static void write(File file, String content) throws IOException
      Writes the given content into the given file using the default charset
      Parameters:
      file - The file
      content - The content
      Throws:
      IOException - If any error occurs
    • checkWritable

      public static boolean checkWritable(File file)
      Checks if the given file can be written to. If the file does not exist checks if it can be created.
      Parameters:
      file - The file to check
      Returns:
      true, if the file can be written to
    • checkParentWritable

      public static boolean checkParentWritable(File file)
      Checks if the parent file can be written to.
      Parameters:
      file - The file to check
      Returns:
      true, if the parent can be written to