out put a file from a InputStream to path and the out file is named as filename. - Android File Input Output

Android examples for File Input Output:Text File

Description

out put a file from a InputStream to path and the out file is named as filename.

Demo Code


//package com.java2s;

import java.io.BufferedOutputStream;
import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import java.util.List;

public class Main {
    private static final int BUFFER_SIZE = 8192;
    protected static byte buf[] = new byte[BUFFER_SIZE];
    private static List<Object> mTmpBuffer = new ArrayList<Object>();

    /**/*from   w  ww.  ja v  a2 s. com*/
     * out put a file from a InputStream to path and the out file is named as
     * filename. if the path is exists and is not a dir will delete the path,
     * and recreate it. if the path is not exists, create the path.
     * 
     * @param is
     *            InputStream contain the file data. point to a single file data
     * @param targetPath
     *            path point to the out file place. The path must end with
     *            system path seprator
     * @param filename
     *            fileName for the out file
     * @return true if out put file successfully. false if failed. if
     *         inputstream is null or the targetPath is null or the filename is
     *         null return false. if the targetPath is empty string or the
     *         filename is empty string return false.
     */
    public static boolean outputFile(InputStream is, String targetPath,
            String filename) {
        if (is == null || targetPath.equals("") || targetPath == null
                || filename.equals("") || filename == null) {
            return false;
        }
        try {
            checkDirectory(targetPath);
            doOutputFile(is, targetPath + filename);
            is.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private static boolean checkDirectory(String dir) {
        File dirObj = new File(dir);
        if (dirObj.exists()) {
            if (!dirObj.isDirectory()) {
                dirObj.delete();
            }
            return false;
        }
        if (!dirObj.exists()) {
            dirObj.mkdirs();
        }
        return true;
    }

    private static void doOutputFile(InputStream is, String filename)
            throws IOException, FileNotFoundException {
        FileOutputStream os = new FileOutputStream(filename);
        BufferedOutputStream bos = new BufferedOutputStream(os, BUFFER_SIZE);
        int len;
        while ((len = is.read(buf, 0, BUFFER_SIZE)) > 0) {
            bos.write(buf, 0, len);
        }
        bos.flush();
        bos.close();
        os.close();
        mTmpBuffer.add(os);
        mTmpBuffer.add(bos);
    }
}

Related Tutorials