Java FileOutputStream Write saveToDisk(String contents, String filename)

Here you can find the source of saveToDisk(String contents, String filename)

Description

save To Disk

License

Open Source License

Declaration

public static void saveToDisk(String contents, String filename) throws IOException 

Method Source Code

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

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

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

public class Main {
    public static String DEFAULT_CHARSET = "UTF-8";

    public static void saveToDisk(String contents, String filename) throws IOException {
        FileOutputStream fos = null;
        try {// w  ww  .  ja va 2 s.  co  m
            fos = new FileOutputStream(filename);
            fos.write(contents.getBytes(DEFAULT_CHARSET));
            fos.flush();
        } finally {
            if (fos != null)
                fos.close();
        }
    }

    /**
     * save primary conetent to local
     * @param is
     * @param filename
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void saveToDisk(InputStream is, String filename) throws FileNotFoundException, IOException {
        FileOutputStream fos = new FileOutputStream(filename);
        byte[] buf = new byte[1024];
        int len = 0;
        while ((len = is.read(buf)) > 0) {
            fos.write(buf, 0, len);
        }
        fos.flush();
        fos.close();
    }
}

Related

  1. saveTo(String path, InputStream in)
  2. saveTo(String path, InputStream in)
  3. saveToBinaryFile(File dest, byte[] data)
  4. saveToDisc(final InputStream fileInputStream, final String fileUploadPath)
  5. saveToDisk(InputStream is, String filename)
  6. saveToEml(Message mail, File emlFile)
  7. saveToFile(byte[] data, String filename)
  8. saveToFile(File file, byte[] buffer)
  9. saveToFile(final String file, final byte[] data, final boolean overwrite)