Java Write String to File writeFile(File file, String data)

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

Description

Writes String data to the given path (UTF-8).

License

Open Source License

Parameter

Parameter Description
file File path to save the data.
data String data to save.

Exception

Parameter Description

Declaration

public static void writeFile(File file, String data) throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/*/*w  w  w  .  j  a v a2 s.c o  m*/
 * Project: Buddata ebXML RegRep
 * Class: FileUtil.java
 * Copyright (C) 2008 Yaman Ustuntas
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.ByteArrayInputStream;
import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**
     * Writes String data to the given path (UTF-8).
     *
     * @param file File path to save the data.
     * @param data String data to save.
     * @throws java.io.FileNotFoundException
     * @throws java.io.IOException
     */
    public static void writeFile(File file, String data) throws FileNotFoundException, IOException {
        writeFile(file, data.getBytes("UTF-8"));
    }

    /**
     * Writes data byte array to the given path.
     *
     * @param file File path to save the data.
     * @param data Data to write in the file.
     * @throws java.io.FileNotFoundException
     * @throws java.io.IOException
     */
    public static void writeFile(File file, byte[] data) throws FileNotFoundException, IOException {
        writeFile(file, new ByteArrayInputStream(data));
    }

    /**
     * Write data from an <code>InputStream</code> to file.
     *
     * @param file File to write to.
     * @param is Stream to write.
     * @throws java.io.IOException
     */
    public static void writeFile(File file, InputStream is) throws IOException {
        FileOutputStream fos = new FileOutputStream(file);
        byte[] buf = new byte[2048];
        int count = 0;
        while ((count = is.read(buf)) > 0) {
            fos.write(buf, 0, count);
        }
        fos.flush();
        fos.close();
        is.close();
    }
}

Related

  1. writeFile(File file, String content, String charsetName)
  2. writeFile(File file, String contents)
  3. writeFile(File file, String contents)
  4. writeFile(File file, String data)
  5. writeFile(File file, String data)
  6. writeFile(File file, String data, boolean append)
  7. writeFile(File file, String dataString)
  8. writeFile(File file, String text)
  9. writeFile(File file, String text)