Java FileWriter Write saveMap(String filename, Map map)

Here you can find the source of saveMap(String filename, Map map)

Description

Saves a map to a file with the given name -- by default, calls the corresponding saveMap() function with a "reverse" parameter of false.

License

Open Source License

Parameter

Parameter Description
filename The name of the file to which to save.
map The String-to-String map to save.

Exception

Parameter Description
IOException an exception

Declaration

public static void saveMap(String filename, Map<String, String> map) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.*;
import java.io.*;

public class Main {
    /**//from  ww  w .j a va  2 s.co  m
     * Saves a map to a file with the given name -- by default, calls the corresponding 
     * saveMap() function with a "reverse" parameter of <i>false</i>.
     * 
     * @param filename The name of the file to which to save.
     * @param map The String-to-String map to save.
     * @throws IOException
     */
    public static void saveMap(String filename, Map<String, String> map) throws IOException {
        saveMap(filename, map, false);
    }

    /**
     * Saves a map to a file with the given name.
     * 
     * @param filename The name of the file to which to save.
     * @param map The String-to-String map to save.
     * @param reverse If true, saves a map not of key:value, but of value:key.
     * @throws IOException
     */
    public static void saveMap(String filename, Map<String, String> map, boolean reverse) throws IOException {

        BufferedWriter w = new BufferedWriter(new FileWriter(filename));
        for (String k : map.keySet()) {
            String v = map.get(k);
            if (reverse) {
                w.write(v + "\t" + k + "\n");
            } else {
                w.write(k + "\t" + v + "\n");
            }
        }
        w.close();
    }
}

Related

  1. saveIntNodes2File(int[] nodes, String fileName)
  2. saveList(String listName, Set list)
  3. saveListStringToFile(String filePath, List listString)
  4. saveListToFile(ArrayList list, String filePath)
  5. saveMap(Map map, String filePath)
  6. saveMappingFile(File mappingFile, Map messageMap)
  7. saveMeasures(String appDirPath, String fileName, double[] measures)
  8. saveMetaClassToFile(File baseDir, String clazzDef, String metaPackageName, Class fromClazz)
  9. saveMetadata(Map metadata, File file)