Example usage for java.io FileOutputStream flush

List of usage examples for java.io FileOutputStream flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes this output stream and forces any buffered output bytes to be written out.

Usage

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();/*w w  w . j a v  a 2s  .  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:com.ikon.util.FileUtils.java

/**
 * Copy InputStream to File./*  ww  w .j  a  va 2  s.c  o m*/
 */
public static void copy(InputStream input, File output) throws IOException {
    FileOutputStream fos = new FileOutputStream(output);
    IOUtils.copy(input, fos);
    fos.flush();
    fos.close();
}

From source file:com.ikon.util.FileUtils.java

/**
 * Copy Reader to File.//from  www. j av a2s.  com
 */
public static void copy(Reader input, File output) throws IOException {
    FileOutputStream fos = new FileOutputStream(output);
    IOUtils.copy(input, fos);
    fos.flush();
    fos.close();
}

From source file:Main.java

public static boolean writeStringToFile(String fileName, String content) {
    File file = new File(fileName);
    if (content == null)
        return false;

    if (!file.exists()) {
        String filepath = file.getAbsolutePath();
        String path = getParentPath(filepath);
        File dir = new File(path);
        dir.mkdirs();//from   w ww .  j a v  a2s  . com
    }
    FileOutputStream fos = null;
    byte[] data;
    try {
        fos = new FileOutputStream(file, false);
        data = content.getBytes();
        fos.write(data, 0, data.length);
        fos.flush();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (null != fos) {
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        data = null;
        fos = null;
    }
    return false;
}

From source file:ZipHandler.java

/**
 * unzipps a zip file placed at <zipURL> to path <xmlURL>
 * @param zipURL/*from  w ww.ja v a2s . c o  m*/
 * @param xmlURL
 */
public static void unStructZip(String zipURL, String xmlURL) throws IOException {
    FileInputStream fis = new FileInputStream(new File(zipURL));
    ZipInputStream zis = new ZipInputStream(fis);
    FileOutputStream fos = new FileOutputStream(xmlURL);
    ZipEntry ze = zis.getNextEntry();
    writeInOutputStream(zis, fos);
    fos.flush();
    fis.close();
    fos.close();
    zis.close();
}

From source file:Main.java

public static void saveBitmapForJPG(Bitmap bitmap, String file) {
    File f = new File(file);
    FileOutputStream fOut = null;
    try {//w  w w.  ja v a 2 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();
    }
}

From source file:Main.java

public static boolean saveBitmap2File(Bitmap bitmap, File file) {
    FileOutputStream fos = null;
    try {//from   w  w  w  .  j a v  a 2s  .co m
        file.deleteOnExit();
        file.createNewFile();
        fos = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    return true;
}

From source file:Main.java

public static void saveMyBitmap(Bitmap mBitmap) {
    File f = new File(FileSavePath + "qrcode.png");
    try {//  w w w.j av a  2 s  . c om
        f.createNewFile();
    } catch (IOException e) {
    }
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(f);
    } catch (Exception e) {
        e.printStackTrace();
    }
    mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    try {
        fOut.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

public static boolean putKeyValues(Context context, Hashtable<String, String> keyvalues) {
    try {/*from  w w w.  j a va 2s .  c om*/
        JSONObject json = new JSONObject();

        for (Entry<String, String> entry : keyvalues.entrySet())
            json.put(entry.getKey(), entry.getValue());

        FileOutputStream fos = context.openFileOutput(KEYVALUES, Context.MODE_PRIVATE);
        fos.write(json.toString().getBytes());
        fos.flush();
        fos.close();

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

From source file:Main.java

public static boolean saveObject(Context context, Object data, String filename) {
    FileOutputStream out;
    ObjectOutputStream oout;// w w w.ja v  a 2s .c om
    try {
        out = context.openFileOutput(filename + ".odb", Context.MODE_PRIVATE);
        oout = new ObjectOutputStream(out);
        oout.writeObject(data);
        oout.flush();
        out.flush();
        oout.close();
        out.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}