Esempio n. 1
0
  // TODO: write tests.
  // create target directory, if it does not exist.
  public static void copyDirectory(String sourceDirectoryPath, String targetDirectoryPath) {

    File sourceDirectory = new File(sourceDirectoryPath);
    File targetDirectory = new File(targetDirectoryPath);

    try {
      if (sourceDirectory.isDirectory()) {
        // do recursion
        if (!targetDirectory.exists()) {
          targetDirectory.mkdir();
        }

        String[] children = sourceDirectory.list();
        for (int i = 0; i < children.length; i++) {
          copyDirectory(
              sourceDirectoryPath + "\\" + children[i], targetDirectory + "\\" + children[i]);
        }
      } else {

        // copy the file
        copyFile(sourceDirectory.getAbsolutePath(), targetDirectory.getAbsolutePath());
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Esempio n. 2
0
  /**
   * reads in data from a file.
   *
   * @param numberOfRows
   * @param numberOfColumns
   * @param ignoreFirstLine
   * @return
   */
  public static double[][] readMatrix(
      int numberOfRows, int numberOfColumns, boolean ignoreFirstLine, String fileName) {

    double[][] matrix = new double[numberOfRows][numberOfColumns];

    try {

      FileReader fr = new FileReader(fileName);

      BufferedReader br = new BufferedReader(fr);
      String line;
      StringTokenizer tokenizer;
      String token;

      if (ignoreFirstLine) {
        br.readLine();
      }

      line = br.readLine();
      int rowId = 0;
      while (line != null) {
        tokenizer = new StringTokenizer(line);

        for (int i = 0; i < numberOfColumns; i++) {
          token = tokenizer.nextToken();
          double parsedNumber = Double.parseDouble(token);
          matrix[rowId][i] = parsedNumber;
        }

        if (tokenizer.hasMoreTokens()) {
          // if there are more columns than expected, throw an
          // exception
          throw new RuntimeException("the number of columns is wrong");
        }

        line = br.readLine();
        rowId++;
      }
      if (rowId != numberOfRows) {
        throw new RuntimeException("the number of rows is wrong");
      }

    } catch (RuntimeException e) {
      // just forward the runtime exception
      throw e;
    } catch (Exception e) {
      e.printStackTrace();

      throw new RuntimeException("Error reading the matrix from the file");
    }

    return matrix;
  }
Esempio n. 3
0
  public static Matrix<String> readStringMatrix(
      String fileName, String delim, StringMatrixFilter filter) {
    Matrix<String> matrix = new Matrix<String>();

    try {

      // FileReader fr = new FileReader(fileName);

      FileInputStream fis = new FileInputStream(fileName);
      InputStreamReader isr = new InputStreamReader(fis, "ISO-8859-1");

      BufferedReader br = null;
      if (fileName.toLowerCase(Locale.ROOT).endsWith(".gz")) {
        br = IOUtils.getBufferedReader(fileName);
      } else {
        br = new BufferedReader(isr);
      }

      String line;
      StringTokenizer tokenizer;
      line = br.readLine();
      while (line != null) {
        ArrayList<String> row = new ArrayList<String>();

        if (delim == null) {
          tokenizer = new StringTokenizer(line);
        } else {
          tokenizer = new StringTokenizer(line, delim);
        }

        while (tokenizer.hasMoreTokens()) {
          row.add(tokenizer.nextToken());
        }

        if (filter != null && filter.removeLine(line)) {

        } else {
          matrix.addRow(row);
        }

        line = br.readLine();
      }
    } catch (Exception e) {
      e.printStackTrace();

      throw new Error("Error reading the file: " + fileName);
    }

    return matrix;
  }
Esempio n. 4
0
  /**
   * TODO: write test.
   *
   * @param sourceFilePath
   * @param targetFilePath
   */
  public static void copyFile(String sourceFilePath, String targetFilePath) {
    File sourceFile = new File(sourceFilePath);
    File targetFile = new File(targetFilePath);

    try {
      // copy the file
      InputStream in = new FileInputStream(sourceFile);
      OutputStream out = new FileOutputStream(targetFile);
      byte[] buf = new byte[1024];
      int len;
      while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
      }
      in.close();
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Esempio n. 5
0
  /**
   * Write out a list of Strings
   *
   * <p>after each String in the list a "\n" is added.
   *
   * @param list
   * @param fileName
   */
  public static void writeList(ArrayList<String> list, String fileName) {
    Writer writer = null;
    if (fileName.toLowerCase(Locale.ROOT).endsWith(".gz")) {
      writer = IOUtils.getBufferedWriter(fileName);
    }

    try {
      if (writer == null) {
        FileOutputStream fos = new FileOutputStream(fileName);
        writer = new OutputStreamWriter(fos);
      }
      char[] charArray = Lists.getCharsOfAllArrayItemsWithNewLineCharacterInbetween(list);
      writer.write(charArray);

      writer.flush();
      writer.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Esempio n. 6
0
  public static LinkedList<String> readFileRows(String fileName) {
    LinkedList<String> list = new LinkedList<String>();

    try {

      FileInputStream fis = new FileInputStream(fileName);
      InputStreamReader isr = new InputStreamReader(fis, "ISO-8859-1");

      BufferedReader br = new BufferedReader(isr);
      String line;
      line = br.readLine();
      while (line != null) {
        list.add(line);
        line = br.readLine();
      }
    } catch (Exception e) {
      e.printStackTrace();
      DebugLib.stopSystemAndReportInconsistency();
    }

    return list;
  }