Java BufferedWriter Write writeFile(String filename, Map map)

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

Description

Create a new file and write the contents to it

License

Open Source License

Parameter

Parameter Description
filename the name of the file, including the path
map whose contents to write

Declaration


public static void writeFile(String filename, Map<String, String> map) 

Method Source Code

//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();
            }
        }

    }
}

Related

  1. writeFile(String dir, String file, String content)
  2. writeFile(String directory, String content, String fileName, boolean deleteFileBeforeWriting)
  3. WriteFile(String file)
  4. writeFile(String file, List data)
  5. writeFile(String filename, ArrayList entries)
  6. writeFile(String line, File file)
  7. writefile(String m, String path)
  8. writeFile(String outputLocation, String fileName, List content)
  9. writeFile(String p_name, String p_encoding, String p_content)