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 copyFile(final File srcFile, final File saveFile) {
    File parentFile = saveFile.getParentFile();
    if (!parentFile.exists()) {
        if (!parentFile.mkdirs())
            return false;
    }//from w ww  . ja  va  2 s  .  co m

    BufferedInputStream inputStream = null;
    BufferedOutputStream outputStream = null;
    try {
        inputStream = new BufferedInputStream(new FileInputStream(srcFile));
        outputStream = new BufferedOutputStream(new FileOutputStream(saveFile));
        byte[] buffer = new byte[1024 * 4];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        outputStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        close(inputStream, outputStream);
    }
    return true;
}

From source file:Main.java

public static void saveBackgroundImage(Context ctx, String filePath, Bitmap bitmap, int quality)
        throws IOException {
    if (bitmap != null) {
        File file = new File(filePath.substring(0, filePath.lastIndexOf(File.separator)));
        if (!file.exists()) {
            file.mkdirs();/*w  w w.  ja  v a2 s . co  m*/
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        bitmap.compress(CompressFormat.PNG, quality, bos);
        bos.flush();
        bos.close();
        if (ctx != null) {
            scanPhoto(ctx, filePath);
        }
    }
}

From source file:Main.java

public static void copyFile(String sourcePath, String toPath) {
    File sourceFile = new File(sourcePath);
    File targetFile = new File(toPath);
    createDipPath(toPath);/*from  www . j  a v  a  2s.c  o  m*/
    try {
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;
        try {
            inBuff = new BufferedInputStream(new FileInputStream(sourceFile));

            outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));

            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            outBuff.flush();
        } finally {
            if (inBuff != null)
                inBuff.close();
            if (outBuff != null)
                outBuff.close();
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

private static File saveFile(InputStream is, String destDir, String fileName) throws IOException {
    File dir = new File(destDir);
    fileName = getString(fileName);/*from   www  . j av a2 s.  c  om*/
    if (!dir.exists()) {
        dir.mkdirs();
    }
    BufferedInputStream bis = new BufferedInputStream(is);
    BufferedOutputStream bos = null;
    try {
        File saveFile = new File(destDir, fileName);
        bos = new BufferedOutputStream(new FileOutputStream(saveFile));
        int len = -1;
        while ((len = bis.read()) != -1) {
            bos.write(len);
            bos.flush();
        }
        bos.close();
        bis.close();
        return saveFile;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static boolean save(String fileName, Bitmap bitmap) {
    if (fileName == null || bitmap == null) {
        return false;
    }// www . ja  v  a2  s  .c  o  m
    boolean savedSuccessfully = false;
    OutputStream os = null;
    File imageFile = new File(fileName);
    File tmpFile = new File(imageFile.getAbsolutePath() + ".tmp");
    try {
        if (!imageFile.getParentFile().exists()) {
            imageFile.getParentFile().mkdirs();
        }
        os = new BufferedOutputStream(new FileOutputStream(tmpFile));
        savedSuccessfully = bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (savedSuccessfully && tmpFile != null && !tmpFile.renameTo(imageFile)) {
            savedSuccessfully = false;
        }
        if (!savedSuccessfully) {
            tmpFile.delete();
        }
    }
    return savedSuccessfully;
}

From source file:Main.java

/**
 * creates azip file from an directory with all subfolders
 * @param dir/*from   w w w.j  ava 2 s  .  c o m*/
 * @param zipFileName
 * @throws IOException
 */
public static void createZipFile(String dir, String zipFileName) throws IOException {
    String dirFile = dir + "/" + zipFileName;
    ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(dirFile)));
    try {
        zipDir(dir, zipOut, zipFileName);
    } finally {
        zipOut.close();
    }
}

From source file:Main.java

/**
 * save the bitmap to local,add give a white bg color
 * @param bitmap/*from w  w  w. j a  v  a2  s . c o  m*/
 * @param path
 * @return
 */
public static boolean saveBitmapNoBgToSdCard(Bitmap bitmap, String path) {
    BufferedOutputStream bos = null;
    try {
        File file = new File(path);
        if (file.exists())
            file.delete();
        bos = new BufferedOutputStream(new FileOutputStream(file));

        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        int w_new = w;
        int h_new = h;
        Bitmap resultBitmap = Bitmap.createBitmap(w_new, h_new, Bitmap.Config.ARGB_8888);
        //            Paint paint = new Paint();
        //            paint.setColor(Color.WHITE);
        Canvas canvas = new Canvas(resultBitmap);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitmap, new Rect(0, 0, w, h), new Rect(0, 0, w_new, h_new), null);
        resultBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        bos.flush();
        resultBitmap.recycle();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

From source file:Main.java

public static boolean compress(File file) {
    try {/*from   w ww . j a  v  a  2  s . c om*/
        String fileName = file.getName();
        if (fileName.indexOf(".") != -1)
            fileName = fileName.substring(0, fileName.indexOf("."));
        FileOutputStream f = new FileOutputStream(file.getParent() + "/" + fileName + ".zip");
        CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
        InputStream in = new FileInputStream(file);
        out.putNextEntry(new ZipEntry(file.getName()));
        int len = -1;
        byte buf[] = new byte[1024];
        while ((len = in.read(buf, 0, 1024)) != -1)
            out.write(buf, 0, len);
        out.closeEntry();

        in.close();
        out.close();
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

public static boolean copyFile(File src, File tar) throws Exception {
    if (src.isFile()) {
        InputStream is = new FileInputStream(src);
        OutputStream op = new FileOutputStream(tar);
        BufferedInputStream bis = new BufferedInputStream(is);
        BufferedOutputStream bos = new BufferedOutputStream(op);
        byte[] bt = new byte[1024 * 8];
        int len = bis.read(bt);
        while (len != -1) {
            bos.write(bt, 0, len);/*from  ww  w .j a  v  a 2  s .co m*/
            len = bis.read(bt);
        }
        bis.close();
        bos.close();
    }
    if (src.isDirectory()) {
        File[] f = src.listFiles();
        tar.mkdir();
        for (int i = 0; i < f.length; i++) {
            copyFile(f[i].getAbsoluteFile(), new File(tar.getAbsoluteFile() + File.separator + f[i].getName()));
        }
    }
    return true;
}

From source file:Main.java

public static void saveBitmapToFile(Bitmap bitmap, String _file) throws IOException {
    BufferedOutputStream os = null;
    try {/*ww  w.  j  a v  a 2 s.  c o  m*/
        File file = new File(_file);
        // String _filePath_file.replace(File.separatorChar +
        // file.getName(), "");
        int end = _file.lastIndexOf(File.separator);
        String _filePath = _file.substring(0, end);
        File filePath = new File(_filePath);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        file.createNewFile();
        os = new BufferedOutputStream(new FileOutputStream(file));
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                Log.e("TAG_ERROR", e.getMessage(), e);
            }
        }
    }
}