Example usage for java.io FileOutputStream write

List of usage examples for java.io FileOutputStream write

Introduction

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

Prototype

public void write(byte b[]) throws IOException 

Source Link

Document

Writes b.length bytes from the specified byte array to this file output stream.

Usage

From source file:Main.java

private static void saveBitmap2TempFile(File file, Bitmap bitmap) {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 70 /* ignored for PNG */, bos);
    byte[] bitmapdata = bos.toByteArray();

    // write the bytes in file
    FileOutputStream fos = null;
    try {//from w  w  w  .j ava2 s . c om

        fos = new FileOutputStream(file);

        fos.write(bitmapdata);
        fos.flush();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {

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

    }

}

From source file:Main.java

public static void logProgressReport(Context c, String[] logs) {
    String dir = c.getExternalFilesDir(null).getParent();
    try {//from w  ww  .  j  a va 2  s .  c o  m
        File file = new File(dir + "/sf_reports.log");
        boolean append = false;
        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        } else {
            /*
            if(file.length() >= 2097152){
               // set log file max size to 2mb
               append = false;
            }
             */

        }
        FileOutputStream fos = new FileOutputStream(file, append);

        for (String log : logs) {
            fos.write((log + "\n\n").getBytes());
        }
        fos.close();
    } catch (IOException ioe) {
        Log.e("AndroidUtils", "An exception has occured in logProgressReport!");
        ioe.printStackTrace();
    }
}

From source file:TemplateImporter.java

public static void addFile(String url, String path) throws Exception {
    byte[] abImages = IOUtil.getStreamContentAsBytes(new URL(url).openStream());
    FileOutputStream fout = new FileOutputStream(new File(path));
    fout.write(abImages);
    fout.flush();/*w  w w  . ja va2 s .c o m*/
    fout.close();
}

From source file:Main.java

public static Bitmap saveBitmapToFile(Activity activity, Bitmap bitmap) {
    String mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
    File imageFile = new File(mPath);
    boolean create = imageFile.mkdirs();
    boolean canWrite = imageFile.canWrite();
    Calendar cal = Calendar.getInstance();
    String date = cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DATE);

    String filename = null;//from   w ww . ja  v  a  2 s.c om
    int i = 0;
    while (imageFile.exists()) {
        i++;
        filename = date + "_mandelbrot" + i + ".png";
        imageFile = new File(mPath, filename);
        boolean canWrite2 = imageFile.canWrite();

    }

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        /*resultB*/bitmap.compress(CompressFormat.PNG, 90, bos);
        byte[] bitmapdata = bos.toByteArray();

        //write the bytes in file
        FileOutputStream fos = new FileOutputStream(imageFile);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();

        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(imageFile));
        activity.sendBroadcast(intent);

        displaySuccesToast(activity);
    } catch (FileNotFoundException e) {
        displayFileError(activity);
        e.printStackTrace();
    } catch (IOException e) {
        displayFileError(activity);
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static void put(String key, byte[] value) {
    File file = newFile(key);//  w  w  w . j ava2 s .c o  m

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(file);
        out.write(value);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

From source file:Main.java

public static void imgCacheWrite(Context context, String cacheImgFileName, String imgBase64Str) {
    File cacheImgFile = new File(context.getFilesDir() + "/" + cacheImgFileName);
    if (cacheImgFile.exists()) {
        cacheImgFile.delete();//from  w  w w  .j av a  2 s  .co  m
    }

    FileOutputStream fos;
    try {
        fos = context.openFileOutput(cacheImgFileName, Context.MODE_PRIVATE);
        fos.write(imgBase64Str.getBytes("utf-8"));
        fos.flush();
        fos.close();

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

}

From source file:asia.gkc.vneedu.utils.FileUtil.java

/**
 * ?// w ww. j a  v  a 2 s. co  m
 *
 * @param bytes - ?
 * @param destination - 
 * @return 
 * @throws IOException
 */
public static boolean transferFile(byte[] bytes, File destination) throws IOException {
    FileOutputStream fos = new FileOutputStream(destination);
    fos.write(bytes);
    fos.close();
    return true;
}

From source file:Main.java

public static boolean cache(Context context, String file, byte[] data, int mode) {
    boolean bResult = false;
    if (null != data && data.length > 0) {
        FileOutputStream fos = null;
        try {/*from  ww w.  j  ava 2  s .  c  o  m*/
            fos = context.openFileOutput(file, mode);
            fos.write(data);
            fos.flush();
            bResult = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    return bResult;
}

From source file:Main.java

public static boolean addTemplateFile(InputStream src, File dest, Map replace) {
    try {/*from  w  w w  . ja v  a  2s  .co m*/
        FileOutputStream out = new FileOutputStream(dest);

        String outstr = patchTemplateFile(src, replace);
        out.write(outstr.getBytes());
        out.close();
    } catch (IOException e) {
        return false;
    }
    return true;
}

From source file:foss.filemanager.core.Utils.java

public static File arrayByteToFile(byte[] fileBArray) throws FileNotFoundException, IOException {
    DateFormat df = new SimpleDateFormat("ddMMyyyy-hhmmss-S");
    String name = df.format(new Date());
    String tmp = System.getProperty("java.io.tmpdir");
    File file = new File(tmp, name);
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(fileBArray);
    fos.close();/*ww  w .jav a  2 s.  c  o  m*/
    return file;
}