Java FileWriter Write save(String targetDir, String filename, String textToSave)

Here you can find the source of save(String targetDir, String filename, String textToSave)

Description

Helper for saving a text to file

License

Open Source License

Parameter

Parameter Description
targetDir the target directory
filename the target filename
textToSave the content for the file

Return

true if ok

Declaration

public static boolean save(String targetDir, String filename, String textToSave) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 itemis AG (http://www.itemis.de).
 * 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
 *******************************************************************************/

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    /**/*from www . j  av  a 2 s.  co m*/
     * Helper for saving a text to file
     *
     * @param targetDir   the target directory
     * @param filename    the target filename
     * @param textToSave  the content for the file
     * @return true if ok
     */
    public static boolean save(String targetDir, String filename, String textToSave) {
        // ensure that directory is available
        File dir = new File(targetDir);
        if (!(dir.exists() || dir.mkdirs())) {
            System.err.println("Error: couldn't create directory " + targetDir + "!");
            return false;
        }

        // delete file prior to saving
        File file = new File(targetDir + "/" + filename);
        file.delete();

        // save contents to file
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            out.write(textToSave);
            out.close();
            System.out.println("Created file " + file.getAbsolutePath());
        } catch (IOException e) {
            return false;
        }

        return true;
    }
}

Related

  1. save(List list, String REPO)
  2. save(Map map, String path)
  3. save(String fileName, Object o)
  4. save(String fileName, String dataToSave)
  5. save(String modelo, String filename)
  6. save2DArray(float[][] data, String filePath)
  7. save2File(String fileName, String content)
  8. save_array(double[] array, String fname)
  9. saveAllToFile(String filename, String... strings)