Ejemplo n.º 1
0
  /**
   * Method: Print_Results Purpose: The purpose of this method is to iterate the map of search
   * results and print them out according to project 2 requirements.
   *
   * @param filename : the file you would like search results to print to.
   */
  public void printResults(String filename) {
    lock.acquireReadLock();
    Path path = Paths.get(filename);

    try (BufferedWriter writer = Files.newBufferedWriter(path, Charset.forName("UTF-8"))) {
      for (String query : map.keySet()) {
        writer.write(query);
        writer.newLine();
        for (SearchResults sr : map.get(query)) {
          writer.write('"' + sr.getPath() + '"' + ", ");
          writer.write(sr.getFrequency() + ", " + sr.getPosition());
          writer.newLine();
        }
        writer.newLine();
      }
      writer.newLine();
    } catch (IOException e) {
      System.out.println("The file " + filename + " could not be written out to.");
    }
    lock.releaseReadLock();
  }