Here you can find the source of saveSortedMap(Map, ?> map, File mapFile, String separator, String qualifier, String encoding)
Parameter | Description |
---|---|
map | Map to save. |
mapFile | Output file name. |
separator | Field separator. |
qualifier | Quote character. |
encoding | Character encoding for the file. |
Parameter | Description |
---|---|
IOException | If output file has error. |
public static void saveSortedMap(Map<?, ?> map, File mapFile, String separator, String qualifier, String encoding) throws IOException, FileNotFoundException
//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); } }