Example usage for android.content Context openFileOutput

List of usage examples for android.content Context openFileOutput

Introduction

In this page you can find the example usage for android.content Context openFileOutput.

Prototype

public abstract FileOutputStream openFileOutput(String name, @FileMode int mode) throws FileNotFoundException;

Source Link

Document

Open a private file associated with this Context's application package for writing.

Usage

From source file:org.montanafoodhub.base.Hub.java

protected void writeToFile(Context context, BufferedReader rd, String fileName) {
    try {//from  w  w  w . j  a  va 2  s.  c o m
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
                context.openFileOutput(fileName, Context.MODE_PRIVATE));
        String line = "";
        while ((line = rd.readLine()) != null) {
            outputStreamWriter.write(line + '\n');
        }
        outputStreamWriter.close();
    } catch (IOException e) {
        Log.e(HubInit.logTag, "File (" + fileName + ") write failed: " + e.toString());
    }
}

From source file:net.sileht.lullaby.Utils.java

public boolean save(Context ctx, String name, Object obj) {
    try {//  ww w.  java 2 s. c om
        FileOutputStream pout = ctx.openFileOutput(name, 0);
        (new ObjectOutputStream(pout)).writeObject(obj);
        pout.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:org.blanco.techmun.android.cproviders.MesasFetcher.java

private void saveOnCache(Context context, Mesas mesas) {
    try {//w ww . j  av  a2 s. c o  m
        FileOutputStream mesasFOS = context.openFileOutput("mesas.df", Context.MODE_PRIVATE);
        ObjectOutputStream mesasOOS = new ObjectOutputStream(mesasFOS);
        mesasOOS.writeObject(mesas);
        mesasOOS.close();
        PreferenceManager.getDefaultSharedPreferences(context).edit()
                .putLong("mesas_last_cache_saved", System.currentTimeMillis()).commit();
    } catch (IOException e) {
        Log.e("tachmun", "Error opening cache file for mesas in content provider. " + "No cache will be saved",
                e);
    }
}

From source file:com.lugia.timetable.SubjectList.java

public boolean saveToFile(Context context) {
    try {//from   ww w. ja  va  2  s  .com
        FileOutputStream out = context.openFileOutput(SAVEFILE, Context.MODE_PRIVATE);
        BufferedOutputStream stream = new BufferedOutputStream(out);

        stream.write(generateJSON().toString().getBytes());

        stream.flush();
        stream.close();

        out.close();
    } catch (Exception e) {
        // something went wrong
        Log.e(TAG, "Error on save!", e);

        return false;
    }

    return true;
}

From source file:com.brewcrewfoo.performance.util.Helpers.java

public static void get_assetsScript(String fn, Context c, String prefix, String postfix) {
    byte[] buffer;
    final AssetManager assetManager = c.getAssets();
    try {//from   w  w w.  jav  a 2s .c om
        InputStream f = assetManager.open(fn);
        buffer = new byte[f.available()];
        f.read(buffer);
        f.close();
        final String s = new String(buffer);
        final StringBuilder sb = new StringBuilder(s);
        if (!postfix.equals("")) {
            sb.append("\n\n").append(postfix);
        }
        if (!prefix.equals("")) {
            sb.insert(0, prefix + "\n");
        }
        sb.insert(0, "#!" + Helpers.binExist("sh") + "\n\n");
        try {
            FileOutputStream fos;
            fos = c.openFileOutput(fn, Context.MODE_PRIVATE);
            fos.write(sb.toString().getBytes());
            fos.close();

        } catch (IOException e) {
            Log.d(TAG, "error write " + fn + " file");
            e.printStackTrace();
        }
    } catch (IOException e) {
        Log.d(TAG, "error read " + fn + " file");
        e.printStackTrace();
    }
}

From source file:org.blanco.techmun.android.cproviders.EventosFetcher.java

private void saveOnCache(Context context, Eventos eventos, long mesaId) {
    try {/*from w  w  w .  j  a  v  a 2  s  .  co m*/
        FileOutputStream mesasFOS = context.openFileOutput("eventos_" + mesaId + ".df", Context.MODE_PRIVATE);
        ObjectOutputStream mesasOOS = new ObjectOutputStream(mesasFOS);
        mesasOOS.writeObject(eventos);
        mesasOOS.close();
        PreferenceManager.getDefaultSharedPreferences(context).edit()
                .putLong("eventos_last_refresh", System.currentTimeMillis()).commit();
        //We just cached the events set the preference to not force the refresh again until expiration time
        PreferenceManager.getDefaultSharedPreferences(context).edit()
                .putBoolean("force_eventos_refresh_" + mesaId, false).commit();
    } catch (IOException e) {
        Log.e("tachmun", "Error opening cache file for mesas in content provider. " + "No cache will be saved",
                e);
    }
}

From source file:io.lqd.sdk.model.LQLiquidPackage.java

public void saveToDisk(Context context) {
    LQLog.data("Saving to local storage");
    try {/*from   w  w  w.  j  ava 2 s.co m*/
        FileOutputStream fileOutputStream = context.openFileOutput(LIQUID_PACKAGE_FILENAME + ".vars",
                Context.MODE_PRIVATE);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
        objectOutputStream.writeObject(this);
        objectOutputStream.flush();
        objectOutputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
        LQLog.infoVerbose("Could not save liquid package to file");
    }
}

From source file:com.youTransactor.uCube.mdm.MDMManager.java

public boolean setSSLCertificat(Context context, KeyStore sslKeystore) {
    try {/*from w  w w .  j  a  v  a  2  s.  co m*/
        FileOutputStream out = context.openFileOutput(KEYSTORE_CLIENT_FILENAME, Context.MODE_PRIVATE);
        sslKeystore.store(out, PWD);
        out.close();

        initialize(context);

        return ready;

    } catch (Exception e) {
        return false;
    }
}

From source file:com.noshufou.android.su.util.Util.java

public static final void copyFromAssets(Context context, String source, String destination) throws IOException {

    // read file from the apk
    InputStream is = context.getAssets().open(source);
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);//w w w  .  j a  va2  s . co  m
    is.close();

    // write files in app private storage
    FileOutputStream output = context.openFileOutput(destination, Context.MODE_PRIVATE);
    output.write(buffer);
    output.close();

    Log.d(TAG, source + " asset copied to " + destination);
}

From source file:com.example.shutapp.DatabaseHandler.java

private static boolean downloadDatabase(Context context) {
    try {//  w  w  w  .  java  2s .  com
        Log.d(TAG, "downloading database");
        URL url = new URL(StringLiterals.SERVER_DB_URL);
        /* Open a connection to that URL. */
        URLConnection ucon = url.openConnection();
        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        /*
         * Read bytes to the Buffer until there is nothing more to read(-1).
         */
        ByteArrayBuffer baf = new ByteArrayBuffer(StringLiterals.DATABASE_BUFFER);
        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }

        /* Convert the Bytes read to a String. */
        FileOutputStream fos = null;
        // Select storage location
        fos = context.openFileOutput("db_name.s3db", Context.MODE_PRIVATE);

        fos.write(baf.toByteArray());
        fos.close();
        Log.d(TAG, "downloaded");
    } catch (IOException e) {
        String exception = e.toString();
        Log.e(TAG, "downloadDatabase Error: " + exception);
        return false;
    } catch (Exception e) {
        String exception = e.toString();
        Log.e(TAG, "downloadDatabase Error: " + exception);
        return false;
    }
    return true;
}