Java Text File Write writeStringToFile(File file, String data, String encoding)

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

Description

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

License

Apache License

Parameter

Parameter Description
file the file to write
data the content to write to the file
encoding the encoding to use, <code>null</code> means platform default

Exception

Parameter Description
IOException in case of an I/O error

Declaration

public static void writeStringToFile(File file, String data, String encoding) throws IOException 

Method Source Code


//package com.java2s;
/*/*from w  w  w  .ja  v  a2 s .  co m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Main {
    /**
     * Writes a String to a file creating the file if it does not exist.
     *
     * NOTE: As from v1.3, the parent directories of the file will be created
     * if they do not exist.
     *
     * @param file  the file to write
     * @param data  the content to write to the file
     * @param encoding  the encoding to use, <code>null</code> means platform default
     * @throws IOException in case of an I/O error
     * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
     */
    public static void writeStringToFile(File file, String data, String encoding) throws IOException {
        if (encoding == null) {
            throw new IllegalArgumentException("Must specify encoding");
        }
        OutputStream out = null;
        try {
            out = openOutputStream(file);
            if (data != null) {
                out.write(data.getBytes(encoding));
            }
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException ioe) {
                // ignore
            }
        }
    }

    /**
     * Opens a {@link FileOutputStream} for the specified file, checking and
     * creating the parent directory if it does not exist.
     * <p>
     * At the end of the method either the stream will be successfully opened,
     * or an exception will have been thrown.
     * <p>
     * The parent directory will be created if it does not exist.
     * The file will be created if it does not exist.
     * An exception is thrown if the file object exists but is a directory.
     * An exception is thrown if the file exists but cannot be written to.
     * An exception is thrown if the parent directory cannot be created.
     *
     * @param file  the file to open for output, must not be <code>null</code>
     * @return a new {@link FileOutputStream} for the specified file
     * @throws IOException if the file object is a directory
     * @throws IOException if the file cannot be written to
     * @throws IOException if a parent directory needs creating but that fails
     * @since Commons IO 1.3
     */
    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()) {
                throw new IOException("File '" + file + "' cannot be written to");
            }
        } else {
            File parent = file.getParentFile();
            if (parent != null && !parent.exists()) {
                if (!parent.mkdirs()) {
                    throw new IOException("File '" + file + "' could not be created");
                }
            }
        }
        return new FileOutputStream(file);
    }
}

Related

  1. writeStringToFile(File file, String content)
  2. writeStringToFile(File file, String content)
  3. writeStringToFile(File file, String content)
  4. writeStringToFile(File file, String contents)
  5. writeStringToFile(File file, String data, String encoding)
  6. writeStringToFile(File file, String data, String encoding)
  7. writeStringToFile(File file, String data, String encoding)
  8. writeStringToFile(File file, String s)
  9. writeStringToFile(File file, String str, boolean compress)