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

public static void save2File(byte[] content, File file) throws IOException {
    FileOutputStream outStream = new FileOutputStream(file);
    outStream.write(content);
    outStream.flush();//w  ww .  j  a  v a2s.  c  o  m
    outStream.close();
}

From source file:Main.java

/**
 * Write file to sdcard/* w  w  w.  j ava 2s .  c om*/
 * @param dir
 * @param bt
 * @return
 */
public synchronized static File writeToSDCard(String dir, byte[] bt) {
    File file = null;
    try {
        file = new File(dir);
        if (file.exists()) {
            return file;
        }
        file.createNewFile();

        FileOutputStream fos = new FileOutputStream(file);
        fos.write(bt);
        fos.close();
        return file;
    } catch (Exception e) {
        if (file.exists()) {
            file.delete();
        }
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static File getFile(Context context, Bitmap sourceImg) {
    try {//from   w w  w  .jav  a2 s  . c om
        File f = new File(context.getCacheDir(), System.currentTimeMillis() + "temp.jpg");
        f.createNewFile();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int options = 100;
        sourceImg.compress(Bitmap.CompressFormat.JPEG, options, bos);
        while (bos.toByteArray().length / 1024 > 100) {
            bos.reset();
            options -= 10;
            sourceImg.compress(Bitmap.CompressFormat.JPEG, options, bos);
        }
        byte[] bitmapdata = bos.toByteArray();
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
        return f;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void saveHistory(String extractPath, String md5, int index) {
    try {//from  w w  w. ja  va  2 s .c  om
        FileOutputStream fos = new FileOutputStream(extractPath + "/history_" + md5, false);
        fos.write(("" + index).getBytes());
        fos.close();
    } catch (IOException ignored) {
    }
}

From source file:Main.java

public static void CacheString(String data, String filename, Context ctx) {
    try {/* w  ww . j  a  va  2 s. com*/
        FileOutputStream fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
        fos.write(data.getBytes());
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void write(Context context, byte[] data, String fileName)
        throws FileNotFoundException, IOException {

    File file = getFile(context, fileName);

    if (file != null && data != null) {
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(data);
        fos.close();/*ww  w  .j  ava 2 s  . c o  m*/
    }
}

From source file:Main.java

public static void writeFile(Context context, String text, String fileName) {
    try {/*from  w  w w .j  a  v a2  s .  co m*/
        FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_APPEND);
        fos.write(text.getBytes());
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void writeToPublicDirectory(String filename, byte[] data, String directory,
        String environmentDirectory) throws Exception {
    File publicDirectory = new File(Environment.getExternalStoragePublicDirectory(environmentDirectory),
            directory);/*  w w  w . j a va 2s .c om*/
    boolean result = publicDirectory.mkdirs();
    File targetFile = new File(publicDirectory, filename);
    FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
    fileOutputStream.write(data);
    fileOutputStream.close();
}

From source file:Main.java

public static boolean saveDrawabletoFile(Context c, Drawable d, File file) {
    //create a file to write bitmap data
    try {//from  w  w w.  ja va2  s  .  c om
        file.createNewFile();
        //Convert bitmap to byte array
        Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
        byte[] bitmapdata = bos.toByteArray();

        //write the bytes in file
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
        return true;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return false;
}

From source file:Main.java

/**
 * Saves a file from the given URL using HTTPS to the given filename and returns the file
 * @param link URL to file//from   w  ww.j a  v  a 2s . c  o m
 * @param fileName Name to save the file
 * @return The file
 * @throws IOException Thrown if any IOException occurs
 */
public static void saveFileFromNetHTTPS(URL link, String fileName) throws IOException {
    HttpsURLConnection con = (HttpsURLConnection) link.openConnection();

    InputStream in = new BufferedInputStream(con.getInputStream());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int n = 0;
    while (-1 != (n = in.read(buf))) {
        out.write(buf, 0, n);
    }
    out.close();
    in.close();
    byte[] response = out.toByteArray();

    File f = new File(fileName);
    if (f.getParentFile() != null) {
        if (f.getParentFile().mkdirs()) {
            System.out.println("Created Directory Structure");
        }
    }

    FileOutputStream fos = new FileOutputStream(f);
    fos.write(response);
    fos.close();
}