Java Write String to File writeFile(String contents, File f)

Here you can find the source of writeFile(String contents, File f)

Description

Writes a string to a file.

License

Open Source License

Parameter

Parameter Description
contents the string to write
f the file to write

Exception

Parameter Description
RuntimeException if something goes wrong

Declaration

public static void writeFile(String contents, File f) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * ALMA - Atacama Large Millimeter Array
 * Copyright (c) ESO - European Southern Observatory, 2011
 * (in the framework of the ALMA collaboration).
 * All rights reserved.//from   w  w  w  .  ja v a2s  .  c  o m
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *******************************************************************************/

import java.io.File;

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

public class Main {
    /**
     * Writes a string to a file.
     * 
     * @param contents the string to write
     * @param f the file to write
     * @throws RuntimeException if something goes wrong
     */
    public static void writeFile(String contents, File f) {
        FileWriter fw = null;
        try {
            fw = new FileWriter(f);
            fw.write(contents);

        } catch (IOException e) {
            throw new RuntimeException(
                    "workdir is " + System.getProperty("user.dir") + ", couldn't write contents to file: " + e);

        } finally {
            try {
                fw.close();
            } catch (Exception exc) {
            }
        }
    }
}

Related

  1. writeFile(String content, String path)
  2. writeFile(String content, String path)
  3. writeFile(String content, String path)
  4. writeFile(String content, String targetfileName)
  5. writeFile(String content, String writePath, String charCoder)
  6. writeFile(String contents, File file)
  7. writeFile(String contents, String filename)
  8. writeFile(String contents, String fileName)
  9. writeFile(String data, File file)