Java FileWriter Write saveFile(File file, String text, boolean append)

Here you can find the source of saveFile(File file, String text, boolean append)

Description

save File

License

Open Source License

Declaration

public static void saveFile(File file, String text, boolean append) throws IOException 

Method Source Code

//package com.java2s;
/*//w  w  w.j a  v  a2  s.c om
 * Copyright 2012 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.io.File;

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

public class Main {
    public static void saveFile(File file, String text, boolean append) throws IOException {
        if (file.isDirectory()) {
            throw new IOException(file + ": Is a directory");
        } else if (file.isFile()) {
            FileWriter fileWriter;
            // append text at the end of the file
            if (append)
                fileWriter = new FileWriter(file, true);
            // overwrite the file
            else
                fileWriter = new FileWriter(file, false);

            fileWriter.write(text);
            fileWriter.flush();
            fileWriter.close();
        } else {
            // create a new file and write to it
            FileWriter fileWriter = new FileWriter(file, false);
            fileWriter.write(text);
            fileWriter.flush();
            fileWriter.close();
        }
    }
}

Related

  1. saveEasyQuestImpl(final String quest, final File targetFile)
  2. saveFile(File directory, String fileName, String content)
  3. saveFile(File file, String contents)
  4. saveFile(File file, String data)
  5. saveFile(File file, String fileContent)
  6. saveFile(List contents, String fileName)
  7. saveFile(String filePath, String content)
  8. saveFile(String filePath, String data)
  9. saveFile(String path, Map> setup)