Package PamUtils

Class TxtFileUtils

java.lang.Object
PamUtils.TxtFileUtils

public class TxtFileUtils extends Object
Some functions to load and import data stored in text files (includes .csv files)
Author:
Jamie Macaulay
  • Constructor Details

    • TxtFileUtils

      public TxtFileUtils()
  • Method Details

    • importCSVData

      public static ArrayList<ArrayList<Double>> importCSVData(String filename)
      Load data from a .csv file into a 2D ArrayList.
      Parameters:
      filename -
      Returns:
      a 2D array of values.
    • importTxtData

      public static ArrayList<ArrayList<Double>> importTxtData(String filename, String delimeter)
      Load data from a text file into a 2D ArrayList. Elements will be separated based on the delimiter string input. Note that lines containing NaN values are skipped.
      Parameters:
      filename - - file path to import.
      delimeter - - separate elements in the file using the delimiter. e.g. \\t for tab, \\s+ for space, , for .csv.
      Returns:
      2D ArrayList of rows and columns defined by delimiter.
    • importTxtData

      public static ArrayList<ArrayList<Double>> importTxtData(String filename, String delimeter, boolean nanOk)
      Load data from a text file into a 2D ArrayList. Elements will be separated based on the delimiter string input. Lines containing NaN values are included if nanOk = true, or skipped if nanOk=false. NaN will be replaced with nulls.
      Parameters:
      filename - - file path to import.
      delimeter - - separate elements in the file using the delimiter. e.g. \\t for tab, \\s+ for space, , for .csv.
      nanOk - leave rows containing NaN values if true, skip rows if false
      Returns:
      2D ArrayList of rows and columns defined by delimiter.
    • importTxtToCollection

      public static List<String> importTxtToCollection(String filename)
      Import text data into an array of strings.
      Parameters:
      filename - - file path of file to import.
    • importTxtDataToString

      public static ArrayList<ArrayList<String>> importTxtDataToString(String filePath)
      Load data from a text file into a 2D ArrayList. Elements will be separated based on the delimiter string input. Any line containing NaN will be ignored
      Parameters:
      filePath - - file path to import.
      Returns:
      2D ArrayList of rows and columns defined by delimiter.
    • importTxtDataToString

      public static ArrayList<ArrayList<String>> importTxtDataToString(String filePath, boolean nanOk)
      Load data from a text file into a 2D ArrayList. Elements will be separated based on the delimiter string input.
      Parameters:
      filePath - - file path to import.
      nanOk - leave rows containing NaN values if true, skip rows if false
      Returns:
      2D ArrayList of rows and columns defined by delimiter.
    • importTxtDataToString

      public static ArrayList<ArrayList<String>> importTxtDataToString(String filePath, String delimeter)
      Load data from a text file into a 2D ArrayList. Elements will be separated based on the delimiter string input. Any line containing NaN will be ignored
      Parameters:
      filePath - - file path to import.
      delimeter - - separate elements in the file using the delimiter.
      Returns:
      2D ArrayList of rows and columns defined by delimiter.
    • importTxtDataToString

      public static ArrayList<ArrayList<String>> importTxtDataToString(String filename, String delimeter, boolean nanOk)
      Load data from a text file into a 2D ArrayList. Elements will be separated based on the delimiter string input.
      Parameters:
      filename - - file path to import.
      delimeter - - separate elements in the file using the delimiter.
      nanOk - leave rows containing NaN values if true, skip rows if false
      Returns:
      2D ArrayList of rows and columns defined by delimiter.
    • parseTxtLine

      public static ArrayList<String> parseTxtLine(String line, String delimiter)
      Convert into ArrayList of strings based on a delimiter. Any lines containing NaN are ignored
      Parameters:
      line - - the line to parse
      delimiter - the delimiter to use
      Returns:
      split strings based on delimiter.
    • parseTxtLine

      public static ArrayList<String> parseTxtLine(String line, String delimiter, boolean nanOk)
      Convert into ArrayList of strings based on a delimiter
      Parameters:
      line - - the line to parse
      delimiter - the delimiter to use
      nanOk - leave rows containing NaN values if true, skip rows if false
      Returns:
      split strings based on delimiter.
    • printData

      public static void printData(ArrayList<ArrayList<String>> data)
      Print out data.
      Parameters:
      data -
    • exportTxtData

      public static boolean exportTxtData(String filename, ArrayList<String> dataToWrite)
      Write string data to a file
      Parameters:
      filename - the filename to write to
      dataToWrite - An ArrayList of String objects to write. Each index in the array will be written on a new line
      Returns:
      true if successful, false if an exception was thrown
    • exportMatlab2DArray

      public static String exportMatlab2DArray(double[][] array, int dp)