Java Write String to File writeFile(String filename, String text, boolean create)

Here you can find the source of writeFile(String filename, String text, boolean create)

Description

Write a text file

License

Open Source License

Parameter

Parameter Description
filename the filename (full path) to be written to
text the text to be written
create if true, the directory and any sub-directories where the file resides are created, otherwise the directory must exist first

Exception

Parameter Description
IOException if an error occurs while writing to the file

Declaration

public static void writeFile(String filename, String text, boolean create) throws IOException 

Method Source Code

//package com.java2s;

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

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

import java.io.PrintWriter;

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**/* w w  w.  j  ava 2s . c  o m*/
     * Write a text file
     * 
     * @param filename
     *            the filename (full path) to be written to
     * @param text
     *            the text to be written
     * @param create
     *            if true, the directory and any sub-directories where the file
     *            resides are created, otherwise the directory must exist first
     * @throws IOException
     *             if an error occurs while writing to the file
     */
    public static void writeFile(String filename, String text, boolean create) throws IOException {
        FileWriter fw = null;
        PrintWriter pw = null;

        if (create) {
            createDirectoriesForFile(filename);
        }

        try {
            fw = new FileWriter(filename);
            pw = new PrintWriter(fw);

            pw.print(text);

        } finally {
            if (fw != null) {
                fw.close();
            }
        }
    }

    /**
     * Write a text file
     * 
     * @param filename
     *            the filename (full path) to be written to
     * @param text
     *            the text to be written
     * @param create
     *            if true, the directory and any sub-directories where the file
     *            resides are created, otherwise the directory must exist first
     * @param append
     *            if true, the text will be written to the end of the file
     * @throws IOException
     *             if an error occurs while writing to the file
     */
    public static void writeFile(String filename, String text, boolean create, boolean append) throws IOException {
        PrintWriter pw = null;

        if (create) {
            createDirectoriesForFile(filename);
        }

        try {
            pw = new PrintWriter(new BufferedWriter(new FileWriter(filename, append)));
            pw.print(text);

        } finally {
            if (pw != null) {
                pw.close();
            }
        }
    }

    /**
     * creates, the directory and any sub-directories where the file resides are
     * created
     * 
     * @param filename
     *            the filename (full path) to where to create directories
     */
    public static void createDirectoriesForFile(String filename) {
        List<File> dirs = new ArrayList<File>();
        File f = new File(filename);

        // Determine which directories don't yet exist
        File fDir = f.getParentFile();
        while (!fDir.exists()) {
            dirs.add(fDir);
            fDir = fDir.getParentFile();
            if (fDir == null) {
                break;
            }
        }

        // Create directories in order
        for (int i = dirs.size(); i > 0; i--) {
            File ff = dirs.get(i - 1);
            ff.mkdir();
        }

    }
}

Related

  1. writeFile(String filename, String string)
  2. writeFile(String filename, String text)
  3. writeFile(String filename, String text)
  4. writeFile(String filename, String text)
  5. writeFile(String fileName, String text)
  6. writeFile(String fileName, String text, String characterEncoding)
  7. writeFile(String fileName, String textToSave)
  8. writeFile(String fileName, String[] contents)
  9. writeFile(String fileName, String[] lines)