Java FileWriter Write save(File file, String content)

Here you can find the source of save(File file, String content)

Description

Saves the content to a file.

License

Open Source License

Parameter

Parameter Description
file the file to save to
content the content to save

Return

true if successfully saved

Declaration

public static boolean save(File file, String content) 

Method Source Code

//package com.java2s;
/*/* www .j av  a2  s . c  o  m*/
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program 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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileWriter;

public class Main {
    /**
     * Saves the content to a file.
     * 
     * @param file      the file to save to
     * @param content      the content to save
     * @return         true if successfully saved
     */
    public static boolean save(File file, String content) {
        boolean result;
        BufferedWriter writer;

        writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(file));
            writer.write(content);
            writer.flush();
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (Exception e) {
                    // ignored
                }
            }
        }

        return result;
    }
}

Related

  1. save(File file, String text)
  2. save(File pFile, String pString)
  3. save(final String content, final String file)
  4. save(List list, String REPO)