Java PrintWriter Write saveSortedMap(Map map, File mapFile, String separator, String qualifier, String encoding)

Here you can find the source of saveSortedMap(Map map, File mapFile, String separator, String qualifier, String encoding)

Description

Save map to a file in sorted key order.

License

Open Source License

Parameter

Parameter Description
map Map to save.
mapFile Output file name.
separator Field separator.
qualifier Quote character.
encoding Character encoding for the file.

Exception

Parameter Description
IOExceptionIf output file has error.

Declaration


public static void saveSortedMap(Map<?, ?> map, File mapFile, String separator, String qualifier,
        String encoding) throws IOException, FileNotFoundException 

Method Source Code


//package com.java2s;
/*   Please see the license information at the end of this file. */

import java.io.*;

import java.util.*;

public class Main {
    /**   Save map to a file in sorted key order.
     */*from   w  ww.j a  v  a2 s. c om*/
     *   @param   map            Map to save.
     *   @param   mapFile         Output file name.
     *   @param    separator      Field separator.
     *   @param   qualifier      Quote character.
     *   @param   encoding      Character encoding for the file.
     *
     *   @throws IOException      If output file has error.
     */

    public static void saveSortedMap(Map<?, ?> map, File mapFile, String separator, String qualifier,
            String encoding) throws IOException, FileNotFoundException {
        if (map != null) {
            PrintWriter printWriter = new PrintWriter(mapFile, "utf-8");

            Set<Object> keySet = new TreeSet<Object>(map.keySet());
            Iterator<Object> iterator = keySet.iterator();

            while (iterator.hasNext()) {
                Object key = iterator.next();
                String value = map.get(key).toString();

                printWriter.println(qualifier + key + qualifier + separator + qualifier + value + qualifier);
            }

            printWriter.flush();
            printWriter.close();
        }
    }

    /**   Save map to a file name in sorted key order.
     *
     *   @param   map            Map to save.
     *   @param   mapFileName      Output file name.
     *   @param    separator      Field separator.
     *   @param   qualifier      Quote character.
     *   @param   encoding      Character encoding for the file.
     *
     *   @throws IOException      If output file has error.
     */

    public static void saveSortedMap(Map<?, ?> map, String mapFileName, String separator, String qualifier,
            String encoding) throws IOException, FileNotFoundException {
        saveSortedMap(map, new File(mapFileName), separator, qualifier, encoding);
    }
}

Related

  1. saveMatrixAsADM(AtomicReferenceArray matrix, String path)
  2. saveMineField(File fileName, String mineField)
  3. saveProbabilityData(String file, double[] probabilities, double start, double dt)
  4. saveProcessedDataFile(double[][] data, String file)
  5. saveSet(Set set, File setFile, String encoding)
  6. saveString(String theFilePath, String theString)
  7. saveStringAsFile(String string, String path)
  8. saveStringBufferTofile(String fileName, StringBuffer sb)
  9. saveStrings(final String filename, final String[] sArray)