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:net.giovannicapuano.visualnovel.Memory.java

public static boolean putLastScript(Context context, String script) {
    try {/* w  ww.  j  av a  2s. 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

/**
 * Write a string value to the specified file.
 * //  w w w. ja v  a2  s.co m
 * @param filename The filename
 * @param value The value
 */
public static void writeValue(String filename, String value) {
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(new File(filename), false);
        fos.write(value.getBytes());
        fos.flush();
        // fos.getFD().sync();
    } catch (FileNotFoundException ex) {
        Log.w(TAG, "file " + filename + " not found: " + ex);
    } catch (SyncFailedException ex) {
        Log.w(TAG, "file " + filename + " sync failed: " + ex);
    } catch (IOException ex) {
        Log.w(TAG, "IOException trying to sync " + filename + ": " + ex);
    } catch (RuntimeException ex) {
        Log.w(TAG, "exception while syncing file: ", ex);
    } finally {
        if (fos != null) {
            try {
                Log.w(TAG_WRITE, "file " + filename + ": " + value);
                fos.close();
            } catch (IOException ex) {
                Log.w(TAG, "IOException while closing synced file: ", ex);
            } catch (RuntimeException ex) {
                Log.w(TAG, "exception while closing file: ", ex);
            }
        }
    }

}

From source file:Main.java

public static void intArrayToFile(Context myContext, String filename, int[] array) {
    File root = myContext.getFilesDir();
    File current = new File(root, filename);
    current.delete();/* www  .  ja  v a 2  s. c  o  m*/
    FileOutputStream outputStream;
    try {
        outputStream = myContext.openFileOutput(filename, Context.MODE_APPEND);
        for (int i : array) {
            String s = "" + i;
            outputStream.write(s.getBytes());
            outputStream.write("\n".getBytes());
        }
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.oprisnik.semdroid.utils.FileUtils.java

public static void writeToFile(String filename, byte[] data) {
    FileOutputStream fos = null;
    try {//from   w ww. j  a va2 s. co m
        fos = new FileOutputStream(filename);
        fos.write(data);
        fos.close();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fos);
    }
}

From source file:Main.java

public static boolean writeFile(Context context, String fileName, String content) {
    boolean success = false;
    FileOutputStream fos = null;
    try {//from   w w w.j  av  a2 s  .c  om
        fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        byte[] byteContent = content.getBytes();
        fos.write(byteContent);

        success = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null)
                fos.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    return success;
}

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

public static boolean putKeyValues(Context context, Hashtable<String, String> keyvalues) {
    try {/* www  .  j ava  2 s.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:ota.otaupdates.utils.Utils.java

public static void DownloadFromUrl(String download_url, String fileName) {
    final String TAG = "Downloader";

    if (!isMainThread()) {
        try {/*w w  w.  ja  v  a  2  s . co  m*/
            URL url = new URL(download_url);

            if (!new File(DL_PATH).isDirectory()) {
                if (!new File(DL_PATH).mkdirs()) {
                    Log.e(TAG, "Creating the directory " + DL_PATH + "failed");
                }
            }

            File file = new File(DL_PATH + fileName);

            long startTine = System.currentTimeMillis();
            Log.d(TAG, "Beginning download of " + url.getPath() + " to " + DL_PATH + fileName);

            /*
             * Open a connection and define Streams
             */
            URLConnection ucon = url.openConnection();
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

            /*
             * Read bytes until there is nothing left
             */
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            /* Convert Bytes to a String */
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baf.toByteArray());
            fos.close();

            Log.d(TAG,
                    "Download finished in " + ((System.currentTimeMillis() - startTine) / 1000) + " seconds");
        } catch (Exception e) {
            Log.e(TAG, "Error: " + e);
            e.printStackTrace();
        }
    } else {
        Log.e(TAG, "Tried to run in Main Thread. Aborting...");
    }
}

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  w  w.j  ava  2  s .  co  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 void compressFileIfNeeded(String filePath) {
    File f = new File(filePath);

    Bitmap bitmap;/*from  www  .  j av a 2 s .  c  o m*/
    bitmap = BitmapFactory.decodeFile(filePath);
    int MAX_IMAGE_SIZE = 1000 * 1024;
    int streamLength = (int) f.length();
    if (streamLength > MAX_IMAGE_SIZE) {
        int compressQuality = 105;
        ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
        while (streamLength >= MAX_IMAGE_SIZE && compressQuality > 5) {
            try {
                bmpStream.flush();//to avoid out of memory error
                bmpStream.reset();
            } catch (IOException e) {
                e.printStackTrace();
            }
            compressQuality -= 5;
            bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
            byte[] bmpPicByteArray = bmpStream.toByteArray();
            streamLength = bmpPicByteArray.length;
            Log.d("test upload", "Quality: " + compressQuality);
            Log.d("test upload", "Size: " + streamLength);
        }

        FileOutputStream fo;

        try {
            f.delete();
            f = new File(filePath);
            fo = new FileOutputStream(f);
            fo.write(bmpStream.toByteArray());
            fo.flush();
            fo.close();
            ExifInterface exif = new ExifInterface(f.getAbsolutePath());
            exif.setAttribute(ExifInterface.TAG_ORIENTATION, "6");
            exif.saveAttributes();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.limy.common.graph.GraphUtils.java

/**
 * PNG??????/*from   w ww. j  a  v  a 2  s.  c  o  m*/
 * @param chart ?
 * @param pngFile png
 * @param width width
 * @param height height
 * @throws IOException I/O
 */
public static void writeImagePng(JFreeChart chart, File pngFile, int width, int height) throws IOException {

    BufferedImage image = chart.createBufferedImage(width, height);
    FileOutputStream outStream = new FileOutputStream(pngFile);
    try {
        PngEncoder pngEncoder = new PngEncoder(image);
        pngEncoder.setCompressionLevel(9);
        outStream.write(pngEncoder.pngEncode());
    } finally {
        outStream.close();
    }
}