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:Main.java

public static void save(String filename, String content, Context context) throws Exception {
    FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
    outStream.write(content.getBytes());
    outStream.close();// w  w  w .  jav a  2s . co  m
}

From source file:Main.java

public static void CacheString(String data, String filename, Context ctx) {
    try {//from   w w  w  . ja  va  2  s. c  o  m
        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

/**
 * Get a stream that can be used for serialization
 * //from   w w w  .  ja  va2s . c o  m
 * @param context
 *            We'll write our data to this context private dir
 * @param name
 * @return
 * @throws IOException
 */
public static ObjectOutputStream writeObjectStream(Context context, String name) throws IOException {
    OutputStream s = context.openFileOutput(name, Context.MODE_PRIVATE);

    // I'd prefer to not overwrite the old file, but Context doesn't offer a
    // fileRename option
    return new ObjectOutputStream(s);
}

From source file:Main.java

public static void saveFile(Context context, String name, String text) throws IOException {
    FileOutputStream fos = context.openFileOutput(name, Context.MODE_PRIVATE);
    fos.write(text.getBytes());/*from www .j a va2 s  .  c  om*/
    fos.close();
}

From source file:Main.java

public static void writeToFile(Context context, String file, String content) throws IOException {
    FileOutputStream fos = context.openFileOutput(file, Context.MODE_PRIVATE);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
    bw.write(content);/*from   w w  w  .j  a va 2s. c  o m*/
    bw.flush();
    bw.close();
    fos.close();
}

From source file:Main.java

public static void writeFile(Context context, String text, String fileName) {
    try {//from w  ww .  j  av a2 s. c o  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 saveObject(Context context, String fileName, Object obj) throws IOException {
    FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
    ObjectOutput out = null;//from   w w  w  . j a  v  a  2  s.  co  m
    try {
        out = new ObjectOutputStream(fos);
        out.writeObject(obj);
        out.flush();
    } finally {
        out.close();
        fos.close();
    }

}

From source file:Main.java

public static <T> void writeSerializedObject(Context context, String fileName, T objectToWrite)
        throws IOException {
    FileOutputStream fileOut = context.openFileOutput(fileName, Context.MODE_PRIVATE);
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(objectToWrite);//  ww  w  .  jav  a  2  s. c  o  m
    out.close();
    fileOut.close();
}

From source file:Main.java

public static void writeString(Context context, String content, String path) {
    try {//from   w w  w  .ja  va 2  s .  c o m
        FileOutputStream fos = context.openFileOutput(path, Context.MODE_PRIVATE);
        PrintStream ps = new PrintStream(fos);
        ps.print(content);
        ps.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void copyFromPackage(Context context, int ressourceId, String target) throws IOException {
    FileOutputStream lOutputStream = context.openFileOutput(target, 0);
    InputStream lInputStream = context.getResources().openRawResource(ressourceId);
    int readByte;
    byte[] buff = new byte[8048];
    while ((readByte = lInputStream.read(buff)) != -1) {
        lOutputStream.write(buff, 0, readByte);
    }// w w w . jav  a 2s .  co m
    lOutputStream.flush();
    lOutputStream.close();
    lInputStream.close();
}