Write a string to a file using FileWriter - Java java.io

Java examples for java.io:FileWriter

Description

Write a string to a file using FileWriter

Demo Code


//package com.java2s;

import java.io.FileWriter;
import java.io.IOException;

public class Main {


    /**/* w w w .  ja  v a 2  s  .  co  m*/
     * Write a string to a file
     *
     * @param str
     *            string to write
     *
     * @param path
     *            file path
     */
    public static void writeTofile(final String str, final String path) {
        FileWriter fw = null;
        try {
            fw = new FileWriter(path);
            fw.write(str);
        } catch (final IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (final IOException e) {
                }
            }
        }
    }
}

Related Tutorials