Java FileOutputStream Write saveFile(final File file, final String contents, final String encoding)

Here you can find the source of saveFile(final File file, final String contents, final String encoding)

Description

Save the data, represented as a String to a file

License

Open Source License

Parameter

Parameter Description
file The location/name of the file to be saved.
contents The data that is to be written to the file.

Exception

Parameter Description
IOException an exception

Declaration

public static void saveFile(final File file, final String contents, final String encoding) throws IOException 

Method Source Code


//package com.java2s;

import java.io.File;

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

public class Main {
    /**/*from  w  w w  .  j  av a2 s.c o m*/
     * Save the data, represented as a String to a file
     *
     * @param file     The location/name of the file to be saved.
     * @param contents The data that is to be written to the file.
     * @throws IOException
     */
    public static void saveFile(final File file, final String contents, final String encoding) throws IOException {
        saveFile(file, contents.getBytes(encoding));
    }

    /**
     * Save the data, represented as a byte array to a file
     *
     * @param file         The location/name of the file to be saved.
     * @param fileContents The data that is to be written to the file.
     * @throws IOException
     */
    public static void saveFile(final File file, byte[] fileContents) throws IOException {
        if (file.isDirectory()) {
            throw new IOException("Unable to save file contents as a directory.");
        }

        final FileOutputStream fos = new FileOutputStream(file);
        fos.write(fileContents);
        fos.flush();
        fos.close();
    }
}

Related

  1. saveFile(File file, InputStream is)
  2. saveFile(File file, String fileName, String filesDirectory)
  3. saveFile(File file, String savePath)
  4. saveFile(final byte[] bytes, final File target)
  5. saveFile(final File file, final byte[] dataToSave)
  6. saveFile(final InputStream in, final String path, final String fileName)
  7. saveFile(final String filename, final String s1, final String textOut, final String s3)
  8. saveFile(InputStream is, File file)
  9. saveFile(InputStream is, String destDir, String fileName)