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 File getFileFromBytes(byte[] b, String outputFile) {
    BufferedOutputStream stream = null;
    File file = null;/* ww w .j a v  a  2  s  .c o  m*/
    FileOutputStream fstream = null;
    try {
        file = new File(outputFile);
        if (!file.exists()) {
            file.createNewFile();
        }
        fstream = new FileOutputStream(file);
        stream = new BufferedOutputStream(fstream);
        stream.write(b);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (stream != null) {
        try {
            stream.close();
            fstream.close();

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

    }

    System.gc();

    return file;
}

From source file:Main.java

public static boolean copyAssetFile(Context context, String originFileName, String destFilePath,
        String destFileName) {//  w  w  w  .j  a va2 s  . c  om
    InputStream is = null;
    BufferedOutputStream bos = null;
    try {
        is = context.getAssets().open(originFileName);
        File destPathFile = new File(destFilePath);
        if (!destPathFile.exists()) {
            destPathFile.mkdirs();
        }

        File destFile = new File(destFilePath + File.separator + destFileName);
        if (!destFile.exists()) {
            destFile.createNewFile();
        }

        FileOutputStream fos = new FileOutputStream(destFile);
        bos = new BufferedOutputStream(fos);

        byte[] buffer = new byte[256];
        int length = 0;
        while ((length = is.read(buffer)) > 0) {
            bos.write(buffer, 0, length);
        }
        bos.flush();

        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (null != is) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

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

    return false;
}

From source file:Main.java

public static void saveBitmap(Bitmap b) {

    String path = initPath();//from  ww w. j  a  va  2s .  co  m

    SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMdd-hhmmss");
    String dataTake = sDateFormat.format(System.currentTimeMillis());

    String jpegName = path + "/" + "repair" + ".jpg";

    try {
        FileOutputStream fout = new FileOutputStream(jpegName);
        BufferedOutputStream bos = new BufferedOutputStream(fout);
        b.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        bos.flush();
        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Log.e("camera", "pic saved jpegName: " + jpegName);
}

From source file:Main.java

public static void save(String filename, Document document) throws IOException {
    PrintStream out = null;/*  w w  w  .ja  va 2s  . com*/
    try {
        out = new PrintStream(new BufferedOutputStream(new FileOutputStream(filename)), true, "UTF-8");
        //traceNode(document, "");
        print(out, document);
    } catch (Exception ex) {
        throw new IOException(ex.getLocalizedMessage());
    } finally {
        if (out != null)
            try {
                out.close();
            } catch (Exception e) {
                // ignore
            }
    }
}

From source file:Main.java

/**
 * Base64 to File//from   w w  w  .j  a v a 2s .  c  om
 * @param base64Str
 * @param filePath
 * @param fileName
 * @throws FileNotFoundException
 * @throws IOException
 */
public static void saveBase64StringToFile(String base64Str, String filePath, String fileName)
        throws FileNotFoundException, IOException {
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    File file = null;
    try {
        File dir = new File(filePath);
        if (!dir.exists() && dir.isDirectory()) {
            dir.mkdirs();
        }
        file = new File(filePath, fileName);
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        byte[] bfile = Base64.decode(base64Str, Base64.DEFAULT);
        bos.write(bfile);
    } catch (FileNotFoundException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static boolean StoreByteImage(Context mContext, byte[] imageData, int quality, String expName) {

    File sdImageMainDirectory = new File("/sdcard/myImages");
    FileOutputStream fileOutputStream = null;
    String nameFile = "";
    try {//from   w ww.ja  v a  2s .  c  o  m

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 5;

        Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);

        fileOutputStream = new FileOutputStream(sdImageMainDirectory.toString() + "/" + nameFile + ".jpg");

        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

        myImage.compress(CompressFormat.JPEG, quality, bos);

        bos.flush();
        bos.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return true;
}

From source file:Main.java

public static boolean writeBmpToSDCard(Bitmap bmp, File file, int quality) {
    try {/*w  ww  .j  a v  a 2s .co  m*/
        ByteArrayOutputStream baosm = new ByteArrayOutputStream();
        if (file.getPath().toLowerCase(Locale.getDefault()).endsWith(".png")) {
            bmp.compress(Bitmap.CompressFormat.PNG, quality, baosm);
        } else {
            bmp.compress(Bitmap.CompressFormat.JPEG, quality, baosm);
        }
        byte[] bts = baosm.toByteArray();

        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();

        File tempFile = new File(file.getPath() + ".png");

        FileOutputStream fosm = new FileOutputStream(tempFile);
        BufferedOutputStream bos = new BufferedOutputStream(fosm);
        bos.write(bts);
        bos.flush();
        bos.close();
        fosm.close();

        tempFile.renameTo(file);

        return true;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return false;
}

From source file:Main.java

public static void writeUrlToFile(String urlToRead, String folderToWrite, String fileName)
        throws MalformedURLException, IOException {
    URL urlIn = new URL(urlToRead);
    File folderOut = new File(folderToWrite);
    if (!(folderOut.exists() || folderOut.mkdirs())) {
        throw new RuntimeException("could not create folder " + folderToWrite);
    }//  w  w w  .  j  a  v  a 2 s  .c  om
    File fileOut = new File(folderOut, fileName);
    try (InputStream in = new BufferedInputStream(urlIn.openStream());
            OutputStream out = new BufferedOutputStream(new FileOutputStream(fileOut));) {
        transfer(in, out);
    }
}

From source file:Main.java

public static void writeFile(byte[] data, File file) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, false));
    bos.write(data);//from ww w  .  j a v a2  s . co m
    bos.close();
}

From source file:Main.java

public static void saveBitmap(Bitmap b) {

    String path = initPath();/*  w w  w  . j ava2 s  .co m*/

    SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMdd-hhmmss");
    String dataTake = sDateFormat.format(System.currentTimeMillis());

    String jpegName = path + "/" + "myImage.jpg";

    try {
        Log.e(TAG, "save pic");

        FileOutputStream fout = new FileOutputStream(jpegName);
        BufferedOutputStream bos = new BufferedOutputStream(fout);
        b.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        bos.flush();
        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
        Log.e(TAG, e.toString());
    }

    Log.e("camera", "pic saved jpegName: " + jpegName);
}