Java FileWriter Write saveToFile(File file, String string)

Here you can find the source of saveToFile(File file, String string)

Description

Saves string to file.

License

Open Source License

Parameter

Parameter Description
file The file to save string to.
string The string.

Exception

Parameter Description
IOException Signals that an I/O exception has occurred.

Declaration

public static void saveToFile(File file, String string) throws IOException 

Method Source Code


//package com.java2s;
import java.io.BufferedWriter;
import java.io.File;

import java.io.FileOutputStream;

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

public class Main {
    /**/*from  w  w w .j a v  a  2 s  .  com*/
     * Saves string to file.
     * 
     * @param file
     *            The file to save string to.
     * @param string
     *            The string.
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static void saveToFile(File file, String string) throws IOException {
        try (BufferedWriter out = new BufferedWriter(new FileWriter(file))) {
            out.write(string);
        }
    }

    /**
     * Saves bytes to file.
     * 
     * @param file
     *            The file to save bytes to.
     * @param data
     *            The data to save.
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static void saveToFile(File file, byte[] data) throws IOException {
        try (FileOutputStream out = new FileOutputStream(file)) {
            out.write(data);
        }
    }
}

Related

  1. saveTextFile(File file, String text)
  2. saveTextFile(String content, File file, boolean append)
  3. saveTextFile(String contents, File file)
  4. saveTextFile(String fileName, String text)
  5. saveToCache(String md, String in, String res)
  6. saveToFile(File file, String text)
  7. saveToFile(List lines, String pathname)
  8. saveToFile(List list, String filename, boolean append)
  9. saveToFile(String fileName, String resultStr)