Java BufferedWriter Write writeFile(String directory, String content, String fileName, boolean deleteFileBeforeWriting)

Here you can find the source of writeFile(String directory, String content, String fileName, boolean deleteFileBeforeWriting)

Description

write File

License

Creative Commons License

Declaration

public static void writeFile(String directory, String content, String fileName,
            boolean deleteFileBeforeWriting) 

Method Source Code

//package com.java2s;
/** /* w ww .  j  a  v a  2s.  c  o  m*/
 * @author Dimitri Dean DARSEYNE (D3), 
 * Published by Short-Circuit under Creative Commons (CC) Licensing: 
 * Authorship/Paternity, NO Commercial Use, NO Derivative
 * Please check for more informations:
 * http://creativecommons.org/licenses/by-nc-nd/2.0/
 *
 * Auteur Dimitri Dean DARSEYNE (D3),
 * Publi? par Short-Circuit sous license Creative Commons (CC):
 * Paternit?, PAS d'Utilisation Commerciale, pas de D?riv?s/Modifications
 * Pour plus d'informations, se rendre sur:
 * http://creativecommons.org/licenses/by-nc-nd/2.0/fr/ 
 * 
 * @since Short-Circuit 1999
 */

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

import java.io.FileWriter;

public class Main {
    public static void writeFile(String directory, String content, String fileName,
            boolean deleteFileBeforeWriting) {
        try {
            if (deleteFileBeforeWriting)
                deleteFile(directory + fileName);

            BufferedWriter out = new BufferedWriter(new FileWriter(directory + fileName, deleteFileBeforeWriting));
            out.write(content);

            //out.newLine();

            out.close();
        } catch (Exception e) {
            System.out.println("failed to write query");
        }
    }

    public static void writeFile(String content, String fileName, boolean deleteFileBeforeWriting) {
        try {
            if (deleteFileBeforeWriting)
                deleteFile(fileName);

            BufferedWriter out = new BufferedWriter(new FileWriter(fileName, deleteFileBeforeWriting));
            out.write(content);

            //out.newLine();

            out.close();
        } catch (Exception e) {
            System.out.println("failed to write query");
        }
    }

    public static void deleteFile(String fileName) {
        try {
            File file2delete = new File(fileName);
            file2delete.delete();
        } catch (Exception e) {
            System.out.println("failed to write query");
        }
    }
}

Related

  1. writeFile(double[][] toWrite, String outName)
  2. writeFile(File dir, File fileRef, File newFileName, ClassLoader classLoader)
  3. writeFile(File src, File dst)
  4. writeFile(Object msg)
  5. writeFile(String dir, String file, String content)
  6. WriteFile(String file)
  7. writeFile(String file, List data)
  8. writeFile(String filename, ArrayList entries)
  9. writeFile(String filename, Map map)