Module simpa.core

Class FileUtils

java.lang.Object
simpa.core.api.utils.FileUtils

public class FileUtils extends Object
Utilities for writing and reading files.
  • Constructor Details

    • FileUtils

      public FileUtils()
  • Method Details

    • scanDirectory

      public static ArrayList<String> scanDirectory(String outDir)
      Scan the output directory for existing output files
      Parameters:
      outDir - The directory to look at
      Returns:
      List of existing files
    • writeTextFile

      public static void writeTextFile(String file, String string)
      Writes the given string to a text file.
      Parameters:
      file - output file name
      string - the string to write to the file
    • appendToFile

      public static void appendToFile(String file, String string)
      Appends the given string to a text file.
      Parameters:
      file - file to append to
      string - string to append to file
    • writeDoubleArrayToFile

      public static void writeDoubleArrayToFile(String file, double[][] arr, boolean exchangeRowCol)
      Writes the given 2D array to a file.
      Parameters:
      file - - Output file.
      arr - - The array
      exchangeRowCol - - If the exchangeRowCol is true, the rows and coulumns are exchanged.
    • writeVectors

      public static void writeVectors(String f, List<org.apache.commons.math3.geometry.euclidean.threed.Vector3D> vectors)
      Writes given vectors to a file with a given name.
      Parameters:
      f - name of the output file.
      vectors - vectors to write to the file.
    • copyResourceFile

      public static void copyResourceFile(String resourceFileName, String outFile)
      Copies a resource file into the given file.
      Parameters:
      resourceFileName - file in the resources folder.
      outFile - name of the output file.
    • readVectors

      public static List<org.apache.commons.math3.geometry.euclidean.threed.Vector3D> readVectors(String filename, LengthUnit lengthUnit) throws FileNotFoundException
      Reads 3D vectors from a given file.
      Parameters:
      filename - - File to read
      lengthUnit - - Length Unit of the values in the file (MM, CM, M)
      Returns:
      List of vectors in meters
      Throws:
      FileNotFoundException - when the file is not found.
    • readVectors

      public static List<org.apache.commons.math3.geometry.euclidean.threed.Vector3D> readVectors(String filename, int column, double scalingFactor) throws FileNotFoundException
      Reads 3D vectors from a given file starting from the given column.
      Parameters:
      filename - - File to read
      column - - Column number in the file of the first component of the vector. The Vector will be formed of { value in column, value in column+1,value in column+2}
      scalingFactor - - Scaling for the vector.
      Returns:
      List of vectors in meters
      Throws:
      FileNotFoundException - when the file is not found.
    • readVectors

      public static List<org.apache.commons.math3.geometry.euclidean.threed.Vector3D> readVectors(InputStream is, LengthUnit lengthUnit)
      Read vectors from an inputstream.
      Parameters:
      is - inputstream of the file
      lengthUnit - unit for length
      Returns:
      list of Vector3D objects
    • readVectors

      public static List<org.apache.commons.math3.geometry.euclidean.threed.Vector3D> readVectors(InputStream is, int column, double scalingFactor)
      Read vectors from a given column in an inputstream.
      Parameters:
      is - inputstream of the file.
      column - column to be read.
      scalingFactor - factor to scale values with.
      Returns:
      list of Vector3D objects
    • getDoubleArrayFromFile

      public static double[] getDoubleArrayFromFile(String filename) throws IOException
      Reads a double array from a file that is delimited by spaces or new lines.
      Parameters:
      filename - name of the file to read
      Returns:
      double array read from the file
      Throws:
      IOException - when a file is not found or something went wrong while reading
    • getKeyLineFromFile

      public static String getKeyLineFromFile(String filename, String key) throws IOException
      Search a line in a file containing the given key text.
      Parameters:
      filename - The file to be searched.
      key - The key string to be searched for.
      Returns:
      The line in the file which contains the key string. If not found an empty string is returned.
      Throws:
      IOException - If the file can't be read.
    • read2DArray

      public static double[][] read2DArray(InputStream is, int nCol)
      Reads a 2D array of doubles with the specified number of columns.
      Parameters:
      is - -InputStream
      nCol - -Number of columns
      Returns:
      result double[][]
    • getNextValidLine

      public static double[] getNextValidLine(BufferedReader breader, int columns) throws IOException
      Reads the next valid line from the BufferedReader containing the given number of numbers.
      Parameters:
      breader - buffered reader object.
      columns - -The number of expected number columns in a valid line.
      Returns:
      - double array containing the parsed numbers, null if end of file.
      Throws:
      IOException - when there is a problem with the file.
    • read2DArray

      public static double[][] read2DArray(String filename, int nCol) throws FileNotFoundException
      Reads a two-dimensional array from a file.
      Parameters:
      filename - input file.
      nCol - amount of columns to read.
      Returns:
      a two-dimensional array of doubles.
      Throws:
      FileNotFoundException
    • writeByteBufferToFile

      public static void writeByteBufferToFile(ByteBuffer bb, String filename)
      Writes a ByteBuffer to a file with the given filename.
      Parameters:
      bb - - ByteBuffer object.
      filename - - output filename.
    • readByteBufferFromFile

      public static ByteBuffer readByteBufferFromFile(String filename)
      Reads a ByteBuffer object from a file with the given filename
      Parameters:
      filename - - which file to read.
      Returns:
      ByteBuffer extracted from file.
    • countNumberOfColumns

      public static int countNumberOfColumns(String filename)
      Counts the number of columns containing numbers in the file. Text which can't be parsed to a number is ignored.
      Parameters:
      filename - input file
      Returns:
      The count of numbers in the first line which contains a number.
    • countLines

      public static int countLines(String file)
      Counts the lines in given file.
      Parameters:
      file - input file.
      Returns:
      the amount of lines in given file.
    • getLineFromFile

      public static String getLineFromFile(String file, long lineNumber)
      Gets a specific line from a file.
      Parameters:
      file - input file.
      lineNumber - linenumber to read.
      Returns:
      string value of the given linenumber.