Java FileWriter Write saveFile(File file, String contents)

Here you can find the source of saveFile(File file, String contents)

Description

Save the given contents into the given file.

License

Open Source License

Parameter

Parameter Description
file the given file target
contents the given contents

Exception

Parameter Description
IOException if an IOException occurs while saving the file

Declaration

public static void saveFile(File file, String contents) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * //from ww  w. j  a  v  a2s . co m
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

public class Main {
    /**
     * Save the given contents into the given file. The file parent folder must exist.
     * 
     * @param file the given file target
     * @param contents the given contents
     * @throws IOException if an IOException occurs while saving the file
     */
    public static void saveFile(File file, String contents) throws IOException {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(file));
            writer.write(contents);
            writer.flush();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }
}

Related

  1. saveDotGraph(final String dotGraph, final File outputDirectory)
  2. saveDoubleArray2File(Double[] doubleArray, String fileName)
  3. saveDoubleArray2FileAppend(Double[] doubleArray, String fileName)
  4. saveEasyQuestImpl(final String quest, final File targetFile)
  5. saveFile(File directory, String fileName, String content)
  6. saveFile(File file, String data)
  7. saveFile(File file, String fileContent)
  8. saveFile(File file, String text, boolean append)
  9. saveFile(List contents, String fileName)