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 OutPutImage(File file, Bitmap bitmap) {
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();/*from   ww w  .  j  av a  2  s .  c o m*/
    }
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        if (bos != null) {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
            bos.flush();
            fos.flush();
            return true;
        } else {
            return false;
        }
    } catch (IOException e) {
        return false;
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static String saveFile(Bitmap bm, String dirPath, String fileName, int scale) {
    File dirFile = new File(dirPath);
    if (!dirFile.exists()) {
        dirFile.mkdirs();//from  w w  w . ja  v  a2  s. c  o m
    }
    File myCaptureFile = new File(dirPath + fileName);
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(Bitmap.CompressFormat.JPEG, scale, bos);
        bos.flush();
        bos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return dirPath + fileName;
}

From source file:Main.java

public static File getFileFromBytes(String name, String path) {
    byte[] b = name.getBytes();
    BufferedOutputStream stream = null;
    File file = null;// ww  w  .  ja  va  2s. c om
    try {
        file = new File(Environment.getExternalStorageDirectory() + path);
        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 downLoadFile(final String strUrl, final String fileURL, final int bufferLength) {
    BufferedInputStream in = null;
    BufferedOutputStream out = null;
    try {//from  www. ja  v a2  s .  co m

        in = new BufferedInputStream(new URL(strUrl).openStream());
        File img = new File(fileURL);
        out = new BufferedOutputStream(new FileOutputStream(img));
        byte[] buf = new byte[bufferLength];
        int count = in.read(buf);
        while (count != -1) {
            out.write(buf, 0, count);
            count = in.read(buf);
        }
        in.close();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();

    }

}

From source file:Main.java

public static void writeObjectToFile(Object oObject, File destDir, String filename) throws IOException {
    File dest = new File(destDir, filename);
    if (dest.exists())
        dest.delete();//  w  w w . j ava2s .c  o m

    OutputStream outStream = null;
    try {
        outStream = new BufferedOutputStream(new FileOutputStream(dest));
        outStream.write((byte[]) oObject);
    } finally {
        if (outStream != null) {
            outStream.close();
        }
    }
}

From source file:Utils.java

public static void download(String urlStr, File destFile) throws IOException {
    URL srcUrl = new URL(urlStr);

    InputStream input = null;/*from  w  w w .  ja  va2  s . c  om*/
    OutputStream output = null;
    try {
        input = srcUrl.openStream();
        FileOutputStream fos = new FileOutputStream(destFile);
        output = new BufferedOutputStream(fos);
        copyStreams(input, output);
    } finally {
        if (input != null) {
            input.close();
        }
        if (output != null) {
            output.flush();
            output.close();
        }
    }
}

From source file:Main.java

public static void copyFile(File sourceFile, File targetFile) throws IOException {
    BufferedInputStream inBuff = null;
    BufferedOutputStream outBuff = null;
    try {/*from www  .j a  v  a  2  s  .com*/
        inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
        outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
        byte[] buffer = new byte[BUFFER];
        int length;
        while ((length = inBuff.read(buffer)) != -1) {
            outBuff.write(buffer, 0, length);
        }
        outBuff.flush();
    } finally {
        if (inBuff != null) {
            inBuff.close();
        }
        if (outBuff != null) {
            outBuff.close();
        }
    }
}

From source file:Main.java

private static int copyFile(final File src, final File dst) throws IOException {
    int totalBytes = 0;
    final InputStream in = new BufferedInputStream(new FileInputStream(src));
    try {//w  ww . jav  a 2s .co m
        final OutputStream out = new BufferedOutputStream(new FileOutputStream(dst));
        try {
            final byte[] buf = new byte[bufferSize];
            while (true) {
                int count;
                if ((count = in.read(buf)) == -1) {
                    break;
                }
                out.write(buf, 0, count);
                totalBytes += count;
            }
        } finally {
            out.close();
        }
    } finally {
        in.close();
    }
    return totalBytes;
}

From source file:Main.java

public static void copyFile(File oldLocation, File newLocation) throws IOException {
    if (oldLocation.exists()) {
        BufferedInputStream reader = new BufferedInputStream(new FileInputStream(oldLocation));
        BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(newLocation, false));
        try {//from www  .  j av a 2  s .c o  m
            byte[] buff = new byte[8192];
            int numChars;
            while ((numChars = reader.read(buff, 0, buff.length)) != -1) {
                writer.write(buff, 0, numChars);
            }
        } catch (IOException ex) {
            throw new IOException(
                    "IOException when transferring " + oldLocation.getPath() + " to " + newLocation.getPath());
        } finally {
            try {
                if (reader != null) {
                    writer.close();
                    reader.close();
                }
            } catch (IOException ex) {
                Log.e(TAG, "Error closing files when transferring " + oldLocation.getPath() + " to "
                        + newLocation.getPath());
            }
        }
    } else {
        throw new IOException("Old location does not exist when transferring " + oldLocation.getPath() + " to "
                + newLocation.getPath());
    }
}

From source file:Main.java

static public File zipFolder(File srcFolder, String destZipFile) throws Exception {
    final File file = new File(srcFolder, destZipFile);
    final ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
    try {//from   w  w  w .j  ava  2 s  .c om
        addFolderToZip("", srcFolder, zip, destZipFile);
    } finally {
        zip.close();
    }
    return file;
}