Java FileWriter Write saveFormedClustersToFile(Vector names, Collection> clusters, String filename)

Here you can find the source of saveFormedClustersToFile(Vector names, Collection> clusters, String filename)

Description

Takes a list of clusters (a cluster is a set of integers), and maps the integer IDs to the given list of string entity names.

License

Apache License

Parameter

Parameter Description
names The list of entity names in order as read from the file that was clustered.
clusters The list of clusters of entity IDs
filename The path to the file to save in.

Declaration

public static void saveFormedClustersToFile(Vector<String> names, Collection<Set<Integer>> clusters,
        String filename) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.BufferedWriter;

import java.io.FileWriter;
import java.io.IOException;

import java.util.Collection;

import java.util.Set;
import java.util.Vector;

public class Main {
    /**/*from  ww  w  . j a va 2s  .c  om*/
     * Takes a list of clusters (a cluster is a set of integers), and maps the integer
     * IDs to the given list of string entity names.  Saves the string names to file.
     * @param names The list of entity names in order as read from the file 
     *              that was clustered.
     * @param clusters The list of clusters of entity IDs
     * @param filename The path to the file to save in.
     */
    public static void saveFormedClustersToFile(Vector<String> names, Collection<Set<Integer>> clusters,
            String filename) {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
            for (Set<Integer> cluster : clusters) {
                boolean first = true;
                for (Integer nameID : cluster) {
                    if (!first)
                        writer.write(", ");
                    writer.write(names.get(nameID));
                    first = false;
                }
                writer.write("\n");
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Takes a list of clusters (a cluster is a set of integers), and maps the integer
     * IDs to the given list of string entity names.  Saves the string names to file.
     * @param names The list of entity names in order as read from the file 
     *              that was clustered.
     * @param clusters The list of clusters of entity IDs
     * @param filename The path to the file to save in.
     */
    public static void saveFormedClustersToFile(Vector<String> names, Set<Integer>[] clusters, String filename) {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
            for (Set<Integer> cluster : clusters) {
                boolean first = true;
                for (Integer nameID : cluster) {
                    if (!first)
                        writer.write(", ");
                    writer.write(names.get(nameID));
                    first = false;
                }
                writer.write("\n");
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Related

  1. saveFile(String[] lines, String savedName, String extension)
  2. saveFileContents(File file, String contents)
  3. saveFileFromString(File file, String encoding, String content)
  4. saveFiles(final List contents, final File outputDirectory, final String outputName, final String fileExtension)
  5. saveFlows(Map differences, String file)
  6. saveGraph(List> graph, String sFileName)
  7. saveIndex(File root_, Hashtable index)
  8. saveInputStreamInFile(InputStream stream, FileWriter f)
  9. saveIntegerNodes2File(Integer[] nodes, String fileName)