Android InputStream Save saveFile(Context context, String fileName, InputStream inputStream)

Here you can find the source of saveFile(Context context, String fileName, InputStream inputStream)

Description

save File

Declaration

public static boolean saveFile(Context context, String fileName,
            InputStream inputStream) 

Method Source Code

//package com.java2s;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;

public class Main {
    public static boolean saveFile(Context context, String fileName,
            InputStream inputStream) {
        File cacheDir = context.getCacheDir();
        File outputFile = new File(cacheDir, fileName);

        writeFile(outputFile, inputStream);
        return true;
    }/*from  w ww  .  ja va  2s .c o m*/

    public static void writeFile(File outputFile, InputStream inputStream) {
        try {
            // write the inputStream to a FileOutputStream
            OutputStream out = new FileOutputStream(outputFile);

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = inputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }

            out.flush();
            out.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

Related

  1. dumpToFile(File file, InputStream inputStream)
  2. saveFile(InputStream in, String fileName)
  3. saveToLocal(InputStream in, String filePath)
  4. writerFromInputStream(InputStream stream, String path, String fileName)
  5. writeFile(File outputFile, InputStream inputStream)
  6. writeXmlToTempFile(InputStream xmlStream, String filePath, String closingTag)
  7. writeToString(InputStream stream)
  8. saveInputStreamAsFile(InputStream inputStream, File target)
  9. saveStream(InputStream is, String savePath)