Here you can find the source of writeFile(String filename, Map
Parameter | Description |
---|---|
filename | the name of the file, including the path |
map | whose contents to write |
public static void writeFile(String filename, Map<String, String> map)
//package com.java2s; //License from project: Open Source License import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Iterator; import java.util.Map; public class Main { /**/*from w w w . j av a2s . co m*/ * Create a new file and write the contents to it * @param filename the name of the file, including the path * @param map whose contents to write */ public static void writeFile(String filename, Map<String, String> map) { File file = new File(filename); BufferedWriter bw = null; Iterator<String> it = map.keySet().iterator(); try { file = new File(filename); if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); bw = new BufferedWriter(fw); while (it.hasNext()) { bw.write(map.get(it.next())); bw.write("\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * Create a new file and write the contents to it * @param filename the name of the file, including the path * @param content the contents to write */ public static void writeFile(String filename, String content) { File file = new File(filename); BufferedWriter bw = null; try { file = new File(filename); if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); bw = new BufferedWriter(fw); bw.write(content); } catch (IOException e) { e.printStackTrace(); } finally { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } }