Java Text File Write writeStringToFile(String content, File file)

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

Description

Writes a string to a file creating the file if it does not exist.

License

Open Source License

Parameter

Parameter Description
content the content to write to the file
file the file to write into

Exception

Parameter Description
IOException an exception

Declaration

public static File writeStringToFile(String content, File file) throws IOException 

Method Source Code


//package com.java2s;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Main {
    /**//  w  ww. jav a2s .  com
     * Writes a string to a file creating the file if it does not exist.
     * 
     * @param content
     *            the content to write to the file
     * @param file the file to write into
     * @return 
     * @throws IOException 
     */
    public static File writeStringToFile(String content, File file) throws IOException {
        write(content, openOutputStream(file));
        return file;
    }

    /**
     * Writes chars from a <code>String</code> to bytes on an
     * <code>OutputStream</code> using the default character encoding of the
     * platform.
     * <p>
     * 
     * @param data
     *            the <code>String</code> to write, null ignored
     * @param output
     *            the <code>OutputStream</code> to write to
     * @throws IOException
     */
    public static void write(String data, OutputStream output) throws IOException {
        if (data != null) {
            output.write(data.getBytes());
        }
    }

    /**
     * Opens a {@link FileOutputStream} for the specified file, checking and
     * creating the parent directory if it does not exist.
     * 
     * @return a new {@link FileOutputStream} for the specified file
     * @throws IOException
     */
    public static FileOutputStream openOutputStream(File file) throws IOException {
        if (file.exists()) {
            if (file.isDirectory()) {
                throw new IOException("File '" + file + "' exists but is a directory");
            }
            if (file.canWrite() == false) {
                throw new IOException("File '" + file + "' cannot be written to");
            }
        } else {
            File parent = file.getParentFile();
            if (parent != null && parent.exists() == false) {
                if (parent.mkdirs() == false) {
                    throw new IOException("File '" + file + "' could not be created");
                }
            }
        }
        return new FileOutputStream(file);
    }
}

Related

  1. writeStringToFile(final File file, final String data)
  2. writeStringToFile(final File file, final String data)
  3. writeStringToFile(final String path, final String output)
  4. writeStringToFile(final String string, final File file)
  5. writeStringToFile(final String text, final File filePath)
  6. writeStringToFile(String content, File file)
  7. writeStringToFile(String content, File outputfile)
  8. writeStringToFile(String content, File targetFile)
  9. writeStringToFile(String content, String encoding, String fileName)