Java OutputStreamWriter Write saveFile(String content, File f, String encoding)

Here you can find the source of saveFile(String content, File f, String encoding)

Description

Method to save String as file in specified encoding/.

License

Open Source License

Parameter

Parameter Description
content which will be saved as it is saved in this String
f file to be saved. No warnings provided
encoding of output byte representation

Exception

Parameter Description
IOException if save fails

Declaration

public static void saveFile(String content, File f, String encoding) throws IOException 

Method Source Code

//package com.java2s;
// License as published by the Free Software Foundation; either

import java.io.BufferedWriter;
import java.io.File;

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

import java.io.OutputStreamWriter;

import java.io.Writer;

public class Main {
    /**/*ww  w .  jav  a 2 s  .  com*/
     * Method to save String as file in UTF-8 encoding.
     * 
     * @param content which will be saved as it is saved in this String
     * @param f file to be saved. No warnings provided
     * @throws IOException if save fails
     */
    public static void saveFile(String content, File f) throws IOException {
        saveFile(content, f, "utf-8");
    }

    /**
     * Method to save String as file in specified encoding/.
     *
     * @param content which will be saved as it is saved in this String
     * @param f file to be saved. No warnings provided
     * @param encoding of output byte representation
     * @throws IOException if save fails
     */
    public static void saveFile(String content, File f, String encoding) throws IOException {
        try (Writer output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), encoding))) {
            output.write(content);
            output.flush();
        }
    }
}

Related

  1. save(String text, File out)
  2. saveCache()
  3. saveDocument(String path, String textToWrite)
  4. saveFile(File file, String whatToSave, String encoding)
  5. saveFile(final File file, final String content)
  6. saveFile(String fname, String text, String encode)
  7. saveFile(String path, String msg)
  8. saveListString(List list, String filename)
  9. saveListText(String filename, List array)