Example usage for java.io FileOutputStream close

List of usage examples for java.io FileOutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this file output stream and releases any system resources associated with this stream.

Usage

From source file:Main.java

public static void copyBitmapFromStream(InputStream in, File file) {
    FileOutputStream fout = null;
    try {/*from  w w w  . j  ava  2  s . co m*/
        fout = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = in.read(buffer)) != -1) {
            fout.write(buffer, 0, len);
        }
        fout.close();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.giovannicapuano.visualnovel.Memory.java

public static boolean putLastScript(Context context, String script) {
    try {// ww w  .  j  av  a  2  s  .  c o  m
        FileOutputStream fos = context.openFileOutput(SCRIPT, Context.MODE_PRIVATE);
        fos.write(script.getBytes());
        fos.flush();
        fos.close();

        return true;
    } catch (IOException e) {
        Utils.error(e);
        Utils.error(context, context.getString(R.string.error_saving_game));
        return false;
    }
}

From source file:Main.java

public static void unZip(String zipFile, String outputFolder) throws IOException {

    byte[] buffer = new byte[1024];

    //create output directory is not exists
    File folder = new File(outputFolder);
    if (!folder.exists()) {
        folder.mkdir();// www  . j  a  va 2 s . c o  m
    }

    //get the zip file content
    ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
    //get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {

        String fileName = ze.getName();
        File newFile = new File(outputFolder + File.separator + fileName);

        //create all non exists folders
        //else you will hit FileNotFoundException for compressed folder
        if (ze.isDirectory())
            newFile.mkdirs();
        else {
            newFile.getParentFile().mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            fos.close();
        }
        ze = zis.getNextEntry();
    }

    zis.closeEntry();
    zis.close();

}

From source file:Main.java

public static boolean installRingtone(final Context context, int resid, final String toneName) {

    String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
    String filename = toneName + ".mp3";
    File fileAlarms = new File(exStoragePath, "/Notifications");
    final File fileTone = new File(fileAlarms, filename);

    if (fileTone.exists())
        return false;

    boolean exists = fileAlarms.exists();
    if (!exists) {
        fileAlarms.mkdirs();/*from w ww. ja  v  a 2 s .  c o m*/
    }

    if (fileTone.exists())
        return false;

    byte[] buffer = null;
    InputStream fIn = context.getResources().openRawResource(resid);
    int size = 0;

    try {
        size = fIn.available();
        buffer = new byte[size];
        fIn.read(buffer);
        fIn.close();
    } catch (IOException e) {
        return false;
    }

    FileOutputStream save;
    try {
        save = new FileOutputStream(fileTone);
        save.write(buffer);
        save.flush();
        save.close();
    } catch (FileNotFoundException e) {
        return false;
    } catch (IOException e) {
        return false;
    }

    MediaScannerConnection.scanFile(context, new String[] { fileTone.getAbsolutePath() }, null,
            new MediaScannerConnection.OnScanCompletedListener() {

                @Override
                public void onScanCompleted(String path, Uri uriTone) {

                    ContentValues values = new ContentValues();
                    values.put(MediaStore.MediaColumns.DATA, fileTone.getAbsolutePath());
                    values.put(MediaStore.MediaColumns.TITLE, toneName);
                    values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mp3");
                    values.put(MediaStore.Audio.Media.ARTIST, "zom");

                    //new
                    values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
                    values.put(MediaStore.Audio.Media.IS_ALARM, true);
                    values.put(MediaStore.Audio.Media.IS_MUSIC, false);

                    // Insert it into the database
                    Uri newUri = context.getContentResolver().insert(uriTone, values);

                    //                RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);

                    //   Settings.System.putString(context.getContentResolver(),
                    //         Settings.System.RINGTONE, uri.toString());

                }
            });

    return true;
}

From source file:Main.java

public static Boolean copyFileToSd(InputStream assetFile, File sdFile) {
    boolean flags = false;
    try {/*from w  w  w  .  j  a va2 s .  co m*/
        FileOutputStream fos = new FileOutputStream(sdFile);
        byte[] buffer = new byte[1024];
        int count;
        while ((count = assetFile.read(buffer)) > 0) {
            fos.write(buffer, 0, count);
        }
        flags = true;
        fos.close();
        assetFile.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return flags;
}

From source file:Main.java

/**
 * Simple copy.  Horrible performance for large files.
 * Good performance for alphabets./*  w ww. j  a v a  2s . c  om*/
 */
public static void copyFile(File source, File dest) throws Exception {
    FileInputStream fis = new FileInputStream(source);
    try {
        FileOutputStream fos = new FileOutputStream(dest);
        try {
            int read = fis.read();
            while (read != -1) {
                fos.write(read);
                read = fis.read();
            }
        } finally {
            fos.close();
        }
    } finally {
        fis.close();
    }
}

From source file:Main.java

public static void saveObject(Context context, String fileName, Object obj) throws IOException {
    FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
    ObjectOutput out = null;//from  ww w  . j  a v a2 s . com
    try {
        out = new ObjectOutputStream(fos);
        out.writeObject(obj);
        out.flush();
    } finally {
        out.close();
        fos.close();
    }

}

From source file:Main.java

public static void copyAssets(Context context, String assetsName, String destFilePath) throws IOException {
    File file = new File(destFilePath);
    FileOutputStream out = new FileOutputStream(file);

    InputStream in = context.getAssets().open(assetsName);

    byte[] buf = new byte[1024];
    int len;//from  w w  w. java 2s .  com
    while ((len = in.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

From source file:Main.java

public static void saveImage(Context context, String fileName, Bitmap bitmap, int quality) throws IOException {
    if (bitmap == null || fileName == null || context == null)
        return;/*from   ww  w  .j  a  v  a  2 s. c  om*/

    FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, quality, stream);
    byte[] bytes = stream.toByteArray();
    fos.write(bytes);
    fos.close();
}

From source file:Main.java

public static void saveBitmapForJPG(Bitmap bitmap, String file) {
    File f = new File(file);
    FileOutputStream fOut = null;
    try {//from w  ww  . j  a  va2  s. c o m
        fOut = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
    try {
        fOut.flush();
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}