Example usage for java.io BufferedOutputStream BufferedOutputStream

List of usage examples for java.io BufferedOutputStream BufferedOutputStream

Introduction

In this page you can find the example usage for java.io BufferedOutputStream BufferedOutputStream.

Prototype

public BufferedOutputStream(OutputStream out) 

Source Link

Document

Creates a new buffered output stream to write data to the specified underlying output stream.

Usage

From source file:Main.java

public static boolean saveFile(Bitmap bm, String path) {
    if (bm == null || path == null)
        return false;
    File myCaptureFile = new File(path);
    if (myCaptureFile.exists()) {
        myCaptureFile.delete();/*from  ww  w.  j  a  v a2s.c om*/
    }
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
        bos.flush();
        bos.close();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static File getFileFromBytes(byte[] b, String outputFile) {
    BufferedOutputStream stream = null;
    File file = null;/* w w  w.  j  a  v  a2s .  c om*/
    try {
        file = new File(outputFile);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        FileOutputStream fstream = new FileOutputStream(file);
        stream = new BufferedOutputStream(fstream);
        stream.write(b);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    return file;
}

From source file:Main.java

public static void copyDatabase2FileDir(Context context, String dbName) {
    InputStream stream = null;//from w w w.ja  va  2s. c  om
    BufferedOutputStream outputStream = null;
    try {

        stream = context.getAssets().open(dbName);
        File file = new File(context.getFilesDir(), dbName);

        outputStream = new BufferedOutputStream(new FileOutputStream(file));
        int len = 0;
        byte[] buf = new byte[1024];
        while ((len = stream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
            outputStream.flush();
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static void compressAFileToZip(File zip, File source) throws IOException {
    Log.d("source: " + source.getAbsolutePath(), ", length: " + source.length());
    Log.d("zip:", zip.getAbsolutePath());
    zip.getParentFile().mkdirs();/*  w w  w .ja  v  a 2 s . c  o  m*/
    File tempFile = new File(zip.getAbsolutePath() + ".tmp");
    FileOutputStream fos = new FileOutputStream(tempFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ZipOutputStream zos = new ZipOutputStream(bos);
    ZipEntry zipEntry = new ZipEntry(source.getName());
    zipEntry.setTime(source.lastModified());
    zos.putNextEntry(zipEntry);

    FileInputStream fis = new FileInputStream(source);
    BufferedInputStream bis = new BufferedInputStream(fis);
    byte[] bArr = new byte[4096];
    int byteRead = 0;
    while ((byteRead = bis.read(bArr)) != -1) {
        zos.write(bArr, 0, byteRead);
    }
    zos.flush();
    zos.closeEntry();
    zos.close();
    Log.d("zipEntry: " + zipEntry, "compressedSize: " + zipEntry.getCompressedSize());
    bos.flush();
    fos.flush();
    bos.close();
    fos.close();
    bis.close();
    fis.close();
    zip.delete();
    tempFile.renameTo(zip);
}

From source file:Main.java

/**
 *  Move the file in oldLocation to newLocation.
 *//*from   www. j  a  v a 2s  . c  o m*/
public static void moveFile(String tempLocation, String newLocation) throws IOException {
    File oldLocation = new File(tempLocation);
    if (oldLocation != null && oldLocation.exists()) {
        BufferedInputStream reader = new BufferedInputStream(new FileInputStream(oldLocation));
        BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(newLocation, false));
        try {
            byte[] buff = new byte[8192];
            int numChars;
            while ((numChars = reader.read(buff, 0, buff.length)) != -1) {
                writer.write(buff, 0, numChars);
            }
        } catch (IOException ex) {
        } finally {
            try {
                if (reader != null) {
                    writer.close();
                    reader.close();
                }
            } catch (IOException ex) {
            }
        }
    } else {
    }
}

From source file:Main.java

public static void compressDir(File file) throws IOException {
    FileOutputStream f = new FileOutputStream(file.getParent() + file.getName() + ".zip");
    CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));

    compressDir(file, out, file.getAbsolutePath());

    out.flush();/*w  ww.j a v a2 s.  c  om*/
    out.close();
}

From source file:Main.java

/**
 * Copies a file from res/raw to destination (typically context.getFilesDir())
 *//*www.  j a  v a  2s  .com*/
public static void copyInputFileStreamToFilesystem(InputStream in, String outputFilePathName)
        throws IOException {
    Log.i(TAG, "copyInputFileStreamToFilesystem() outputFilePathName: " + outputFilePathName);
    OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFilePathName));
    byte[] buffer = new byte[4096];
    int len = in.read(buffer);
    while (len != -1) {
        out.write(buffer, 0, len);
        len = in.read(buffer);
    }
    out.close();
}

From source file:Main.java

public static int copyFile(String fileDir, String fileName, byte[] buffer) {
    if (buffer == null) {
        return -2;
    }/*from   www . j  a v a2s . co  m*/

    try {
        File file = new File(fileDir);
        if (!file.exists()) {
            file.mkdirs();
        }
        File resultFile = new File(file, fileName);
        if (!resultFile.exists()) {
            resultFile.createNewFile();
        }
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                new FileOutputStream(resultFile, true));
        bufferedOutputStream.write(buffer);
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
        return 0;

    } catch (Exception e) {
    }
    return -1;
}

From source file:Main.java

public static boolean prepareDex(Context context, File dexInternalStoragePath, String dexFile) {
    BufferedInputStream bis = null;
    OutputStream dexWriter = null;

    try {// w  w  w  .  j  ava 2s  . c  o m
        bis = new BufferedInputStream(context.getAssets().open(dexFile));
        dexWriter = new BufferedOutputStream(new FileOutputStream(dexInternalStoragePath));
        byte[] buf = new byte[BUF_SIZE];
        int len;
        while ((len = bis.read(buf, 0, BUF_SIZE)) > 0) {
            dexWriter.write(buf, 0, len);
        }
        dexWriter.close();
        bis.close();
        return true;
    } catch (IOException e) {
        if (dexWriter != null) {
            try {
                dexWriter.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
        return false;
    }
}

From source file:Main.java

public static long downloadFileFromUrl(String urlPath, File file) {
    long size = 0;
    try {/*from  w  w  w.j av  a 2  s .c om*/
        URL url = new URL(urlPath);
        HttpURLConnection httpurlconnection = (HttpURLConnection) url.openConnection();
        BufferedInputStream bufferedinputstream = new BufferedInputStream(httpurlconnection.getInputStream());
        BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(file));
        int i;
        while ((i = bufferedinputstream.read()) != -1) {
            bufferedoutputstream.write(i);
        }
        bufferedinputstream.close();
        bufferedoutputstream.close();
        httpurlconnection.disconnect();
        size = file.length();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return size;
}