Class FileHelper

java.lang.Object
org.sgrewritten.stargate.util.FileHelper

public final class FileHelper extends Object
Utility class for helping with file reading and writing
  • Method Details

    • getBufferedReader

      public static BufferedReader getBufferedReader(File file) throws IOException
      Gets a buffered reader for reading the given file
      Parameters:
      file -

      The file to read

      Returns:

      A buffered reader for reading the given file

      Throws:
      FileNotFoundException -

      If the given file does not exist

      IOException
    • getBufferedReader

      public static BufferedReader getBufferedReader(File file, String encoding) throws IOException
      Gets a buffered reader for reading the given file
      Parameters:
      file -

      The file to read

      encoding -

      The encoding of the file

      Returns:

      A buffered reader for reading the given file

      Throws:
      IOException -

      If unable to initialize the buffered reader

    • getBufferedWriter

      public static BufferedWriter getBufferedWriter(File file, boolean appendToFile) throws FileNotFoundException
      Gets a buffered writer for writing to the given file
      Parameters:
      file -

      The file to write to

      appendToFile -

      Whether the writer should append to the file

      Returns:

      A buffered writer for writing to the given file

      Throws:
      FileNotFoundException -

      If the given file does not exist

    • getInputStreamForInternalFile

      public static InputStream getInputStreamForInternalFile(String file)
      Gets an input stream from a string pointing to an internal file

      This is used for getting an input stream for reading a file contained within the compiled .jar file. The file should be in the resources directory, and the file path should start with a forward slash ("/") character.

      Parameters:
      file -

      The file to read

      Returns:

      An input stream for the file

    • getBufferedReaderFromInputStream

      public static BufferedReader getBufferedReaderFromInputStream(InputStream inputStream)
      Gets a buffered reader given an input stream
      Parameters:
      inputStream -

      The input stream to read

      Returns:

      A buffered reader reading the input stream

    • readInternalFileToMap

      public static void readInternalFileToMap(String file, Map<String,String> targetMap)
      Reads key, value pairs in an internal file to the given map
      Parameters:
      file -

      The path of the internal file to read

      targetMap -

      The map to store all found values to

    • readStreamToString

      public static String readStreamToString(InputStream stream) throws IOException
      Converts the stream directly into a string, includes the newline character
      Parameters:
      stream -

      The stream to read from

      Returns:

      A String of the file read

      Throws:
      IOException -

      If unable to read the stream

    • listFilesOfInternalDirectory

      public static List<Path> listFilesOfInternalDirectory(String directory) throws IOException, URISyntaxException
      Throws:
      IOException
      URISyntaxException
    • readKeyValuePairs

      public static Map<String,String> readKeyValuePairs(BufferedReader bufferedReader) throws IOException
      Reads key/value pairs from an input stream
      Parameters:
      bufferedReader -

      The buffered reader to read

      Returns:

      A map containing the read pairs

      Throws:
      IOException -

      If unable to read from the stream

    • removeUTF8BOM

      public static String removeUTF8BOM(String string)
      Removes the UTF-8 Byte Order Mark if present
      Parameters:
      string -

      The string to remove the BOM from

      Returns:

      A string guaranteed without a BOM

    • createHiddenFileIfNotExists

      public static File createHiddenFileIfNotExists(String dataFolder, String internalFolder, String fileName) throws IOException
      Creates a hidden file if it does not already exist
      Parameters:
      dataFolder -

      The stargate datafolder

      internalFolder -

      The hidden datafolder

      fileName -

      The name of the file to be created

      Returns:

      The location of the file created

      Throws:
      IOException -

      If unable to create the file