java.lang.Object
simpa.core.api.utils.FileUtils
Utilities for writing and reading files.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidappendToFile(String file, String string) Appends the given string to a text file.static voidcopyResourceFile(String resourceFileName, String outFile) Copies a resource file into the given file.static intcountLines(String file) Counts the lines in given file.static intcountNumberOfColumns(String filename) Counts the number of columns containing numbers in the file.static double[]getDoubleArrayFromFile(String filename) Reads a double array from a file that is delimited by spaces or new lines.static StringgetKeyLineFromFile(String filename, String key) Search a line in a file containing the given key text.static StringgetLineFromFile(String file, long lineNumber) Gets a specific line from a file.static double[]getNextValidLine(BufferedReader breader, int columns) Reads the next valid line from the BufferedReader containing the given number of numbers.static double[][]read2DArray(InputStream is, int nCol) Reads a 2D array of doubles with the specified number of columns.static double[][]read2DArray(String filename, int nCol) Reads a two-dimensional array from a file.static ByteBufferreadByteBufferFromFile(String filename) Reads a ByteBuffer object from a file with the given filenamereadNonNumericalLines(String filename) Reads and returns all non-numerical lines at the beginning of a text file.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.static List<org.apache.commons.math3.geometry.euclidean.threed.Vector3D> readVectors(InputStream is, LengthUnit lengthUnit) Read vectors from an inputstream.static List<org.apache.commons.math3.geometry.euclidean.threed.Vector3D> readVectors(String filename, int column, double scalingFactor) Reads 3D vectors from a given file starting from the given column.static List<org.apache.commons.math3.geometry.euclidean.threed.Vector3D> readVectors(String filename, LengthUnit lengthUnit) Reads 3D vectors from a given file.scanDirectory(String outDir) Scan the output directory for existing output filesstatic voidwriteByteBufferToFile(ByteBuffer bb, String filename) Writes a ByteBuffer to a file with the given filename.static voidwriteDoubleArrayToFile(String file, double[][] arr, boolean exchangeRowCol) Writes the given 2D array to a file.static voidwriteDoubleArrayToStream(BufferedWriter outputStream, double[][] arr, boolean exchangeRowCol) Writes the given 2D array to the output stream.static voidwriteTextFile(String file, String string) Writes the given string to a text file.static voidwriteVectors(String f, List<org.apache.commons.math3.geometry.euclidean.threed.Vector3D> vectors) Writes given vectors to a file with a given name.
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
scanDirectory
Scan the output directory for existing output files- Parameters:
outDir- The directory to look at- Returns:
- List of existing files
-
writeTextFile
Writes the given string to a text file.- Parameters:
file- output file namestring- the string to write to the file
-
appendToFile
Appends the given string to a text file.- Parameters:
file- file to append tostring- string to append to file
-
writeDoubleArrayToFile
Writes the given 2D array to a file.- Parameters:
file- - Output file.arr- - The arrayexchangeRowCol- - If the exchangeRowCol is true, the rows and coulumns are exchanged.
-
writeDoubleArrayToStream
public static void writeDoubleArrayToStream(BufferedWriter outputStream, double[][] arr, boolean exchangeRowCol) throws IOException Writes the given 2D array to the output stream.- Parameters:
outputStream- - Output stream.arr- - The arrayexchangeRowCol- - If the exchangeRowCol is true, the rows and coulumns are exchanged.- Throws:
IOException
-
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
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 readlengthUnit- - 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 readcolumn- - 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 filelengthUnit- 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- The first column to be to start withscalingFactor- factor to scale values with.- Returns:
- list of Vector3D objects
-
getDoubleArrayFromFile
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
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
Reads a 2D array of doubles with the specified number of columns.- Parameters:
is- -InputStreamnCol- -Number of columns- Returns:
- result double[][]
-
getNextValidLine
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
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
Writes a ByteBuffer to a file with the given filename.- Parameters:
bb- - ByteBuffer object.filename- - output filename.
-
readByteBufferFromFile
Reads a ByteBuffer object from a file with the given filename- Parameters:
filename- - which file to read.- Returns:
- ByteBuffer extracted from file.
-
countNumberOfColumns
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
Counts the lines in given file.- Parameters:
file- input file.- Returns:
- the amount of lines in given file.
-
getLineFromFile
Gets a specific line from a file.- Parameters:
file- input file.lineNumber- linenumber to read.- Returns:
- string value of the given linenumber.
-
readNonNumericalLines
Reads and returns all non-numerical lines at the beginning of a text file. Non-numerical lines include comments (starting with #), empty lines, and lines that cannot be parsed as space-separated doubles. Stops at the first numerical line.- Parameters:
filename- the name of the file to read.- Returns:
- a list of strings containing the non-numerical lines from the start of the file.
- Throws:
IOException- if an I/O error occurs.
-