Java Text File Write writeStringToFile(@Nonnull String string, @Nonnull final File file)

Here you can find the source of writeStringToFile(@Nonnull String string, @Nonnull final File file)

Description

Writes a String to a File .

License

Mozilla Public License

Parameter

Parameter Description
string String to write
file file to write to

Exception

Parameter Description
IOException If an I/O error occurs

Declaration

public static void writeStringToFile(@Nonnull String string, @Nonnull final File file) throws IOException 

Method Source Code


//package com.java2s;
/* Copyright (c) 2013 dumptruckman
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStreamWriter;

import javax.annotation.Nonnull;

public class Main {
    /**// ww w  .  j a  v  a 2 s .  c  om
     * Writes a {@link String} to a {@link File}.
     *
     * @param string String to write
     * @param file file to write to
     *
     * @throws IOException If an I/O error occurs
     */
    public static void writeStringToFile(@Nonnull String string, @Nonnull final File file) throws IOException {
        checkNotNull(string, "string cannot be null.");
        checkNotNull(file, "file cannot be null.");

        try (OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8")) {
            string = string.replaceAll("\n", System.getProperty("line.separator"));
            out.write(string);
        }
    }
}

Related

  1. writeStringInto(String s, File outputFile)
  2. writeStringListToFile(File file, ArrayList lines)
  3. writeStringListToFile(File file, List lines)
  4. writeStringTo(File outFile, String data)
  5. writeStringTo(String _value, File _target)
  6. writeStringToFile(CharSequence contents, String filename)
  7. writeStringToFile(File f, String content, boolean append)
  8. writeStringToFile(File f, String s)
  9. writeStringToFile(File f, String s)