Java Properties Save saveOccurrencesAsText(String fileName, TreeMap distribution, int frequency, char[] ignore)

Here you can find the source of saveOccurrencesAsText(String fileName, TreeMap distribution, int frequency, char[] ignore)

Description

Saves those occurrences of a frequency distribution which have the specified frequency into a text file.

License

Open Source License

Parameter

Parameter Description
fileName the file name (without extension) in which the text is stored
distribution the frequency distribution
frequency the frequency which filters the occurrences
ignore list of characters to be ignored

Declaration

public static void saveOccurrencesAsText(String fileName,
        TreeMap<String, Integer> distribution, int frequency,
        char[] ignore) 

Method Source Code

//package com.java2s;
/*/*from  w  w w  . ja va2s .c o m*/
 * (c) 2011-12 Andreas de Vries
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see http://www.gnu.org/licenses
 * or write to the Free Software Foundation,Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301  USA
 * 
 * As a special exception, the copyright holders of this program give you permission 
 * to link this program with independent modules to produce an executable, 
 * regardless of the license terms of these independent modules, and to copy and 
 * distribute the resulting executable under terms of your choice, provided that 
 * you also meet, for each linked independent module, the terms and conditions of 
 * the license of that module. An independent module is a module which is not derived 
 * from or based on this program. If you modify this program, you may extend 
 * this exception to your version of the program, but you are not obligated to do so. 
 * If you do not wish to do so, delete this exception statement from your version.
 */

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

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

import java.util.Properties;
import java.util.TreeMap;

public class Main {
    /** The name of the property file. This file stores configuration data
     *  for the gene package programs, for example the directory which has 
     *  been opened recently for loading or saving files.
     */
    static final String propertyFile = System.getProperty("user.home")
            + System.getProperty("file.separator") + "genes.xml";

    /** Saves those occurrences of a frequency distribution which have the specified 
     *  frequency into a text file.
     *  @param fileName the file name (without extension) in which the text is stored
     *  @param distribution the frequency distribution
     *  @param frequency the frequency which filters the occurrences
     */
    public static void saveOccurrencesAsText(String fileName,
            TreeMap<String, Integer> distribution, int frequency) {
        StringBuilder out = new StringBuilder("Occurrences with frequency "
                + frequency + " (" + fileName + "):\n");
        int count = 1;
        int linebreak = 2;
        for (String tuple : distribution.keySet()) {
            linebreak = 80 / (tuple.length() + 2);
            break;
        }

        for (String tuple : distribution.keySet()) {
            if (distribution.get(tuple) == frequency) {
                out.append(tuple + ", ");
                if (count % linebreak == 0)
                    out.append("\n");
                count++;
            }
        }
        save(fileName + ".txt", out);
    }

    /** Saves those occurrences of a frequency distribution which have the specified 
     *  frequency into a text file.
     *  @param fileName the file name (without extension) in which the text is stored
     *  @param distribution the frequency distribution
     *  @param frequency the frequency which filters the occurrences
     *  @param ignore list of characters to be ignored
     */
    public static void saveOccurrencesAsText(String fileName,
            TreeMap<String, Integer> distribution, int frequency,
            char[] ignore) {
        StringBuilder out = new StringBuilder("Occurrences with frequency "
                + frequency + " (" + fileName + "):\n");
        int i;
        int count = 1;
        boolean ignoreTuple;
        int linebreak = 2;
        for (String tuple : distribution.keySet()) {
            linebreak = 80 / (tuple.length() + 2);
            break;
        }

        for (String tuple : distribution.keySet()) {
            if (distribution.get(tuple) == frequency) {
                ignoreTuple = false;
                for (i = 0; i < ignore.length; i++) {
                    if (tuple.contains("" + ignore[i])) {
                        ignoreTuple = true;
                        break;
                    }
                }
                if (!ignoreTuple) {
                    out.append(tuple + ", ");
                    if (count % linebreak == 0)
                        out.append("\n");
                    count++;
                }
            }
        }
        save(fileName + ".txt", out);
    }

    /** Saves the specified character sequence.
     *  @param fileName name of the file
     *  @param string a sequence of characters
     */
    public static void save(String fileName, CharSequence string) {
        File file;
        try {
            Properties props = new Properties();
            props.loadFromXML(new FileInputStream(propertyFile));
            file = new File(props.getProperty("currentDirectory")
                    + System.getProperty("file.separator") + fileName);
        } catch (Exception e) {
            file = new File(fileName);
        }

        // open the file:
        FileWriter output = null;
        try {
            output = new FileWriter(file);
            try {
                output.write(string.toString());
                output.flush();
                System.out.println(file + " saved!");
            } catch (EOFException eof) {
                // do nothing, the stream will be closed in the finally clause
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                if (output != null)
                    output.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
            try {
                Properties props = new Properties();
                props.setProperty("currentDirectory", file.getParent());
                props.storeToXML(new FileOutputStream(propertyFile), null);
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }
        }
    }
}

Related

  1. saveConfig(File settingsFile, Properties properties)
  2. saveDefaultScriptDirectory(String path)
  3. SaveDeployedObjects(String outputDirectory)
  4. saveInstalledChecksumCache(File dir, Map checksums)
  5. saveMailAtt(String host, String userName, String password, String from, String directory)
  6. saveOutputFile(String prefix, String suffix, String data)
  7. saveParamsToFile(String fileName, String[] params)
  8. saveParamToFile(String fileName, String param, String value)
  9. saveProperties()