Java FileOutputStream Write saveInFile(String filename, String content, String charsetName)

Here you can find the source of saveInFile(String filename, String content, String charsetName)

Description

Save in a file the content of a string.

License

Open Source License

Parameter

Parameter Description
filename The file name.
content The content of the file.
charsetName The charset of the string.

Exception

Parameter Description
Exception an exception

Return

New file instance

Declaration

public static File saveInFile(String filename, String content, String charsetName) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**/*w  ww  .j  a  va  2 s. c  o  m*/
     * Save in a file the content of a string. It's very useful for debug.
     *
     * @param filename The file name.
     * @param content The content of the file.
     * @param charsetName The charset of the string.
     * @return New file instance
     * @throws Exception
     */
    public static File saveInFile(String filename, String content, String charsetName) throws Exception {
        InputStream is = new ByteArrayInputStream(content.getBytes(charsetName));
        byte[] b = new byte[2048];
        int length;
        OutputStream os = new FileOutputStream(filename);
        while ((length = is.read(b)) != -1) {
            os.write(b, 0, length);
        }
        os.close();
        return new File(filename);
    }

    /**
     * Save in a file the content of a string. It's very useful for debug.
     *
     * @param filename The file name.
     * @param content The content of the file.
     * @return New file instance.
     * @throws Exception
     */
    public static File saveInFile(String filename, String content) throws Exception {
        return saveInFile(filename, content, "ISO-8859-1");
    }
}

Related

  1. saveFile(String pathRoot, String subPath, boolean isFoler, InputStream in)
  2. saveFile(String text, File saveFile)
  3. saveFileBinary(final File file, final byte[] data)
  4. SaveFileFromInputStream(InputStream in, String fileName, String path)
  5. SaveFileFromInputStream(InputStream stream, String path, String filename)
  6. saveInputStream(InputStream _input_stream, File _file)
  7. saveInputStream(InputStream is, String filePath)
  8. saveInt16bit(String filename, int[] intData)
  9. saveIntArrayToFile(int[] array, File file)