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:com.kyon.klib.base.KFileUtils.java

public static void writeFile(Context context, String fileName, String writeStr) throws IOException {
    try {/* w  w  w.ja v  a 2  s.  com*/
        FileOutputStream fOut = context.openFileOutput(fileName, 0);
        byte[] bytes = writeStr.getBytes();
        fOut.write(bytes);
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void saveJsonToFile(String toSave, String filename, Activity currActivity) {

    // check if the file exists
    File dir = currActivity.getFilesDir();
    File file = new File(dir, filename);

    if (file.exists()) {
        file.delete();/*ww  w.  j av a 2 s  .com*/
    }

    FileOutputStream outputStream;
    try {
        outputStream = currActivity.openFileOutput(filename, Context.MODE_APPEND);
        outputStream.write(toSave.getBytes());
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.vexsoftware.votifier.crypto.RSAIO.java

/**
 * Saves the key pair to the disk./*from w w  w .  j  ava  2 s. co  m*/
 * 
 * @param directory
 *            The directory to save to
 * @param keyPair
 *            The key pair to save
 * @throws Exception
 *             If an error occurs
 */
public static void save(File directory, KeyPair keyPair) throws Exception {
    PrivateKey privateKey = keyPair.getPrivate();
    PublicKey publicKey = keyPair.getPublic();

    // Store the public key.
    X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(publicKey.getEncoded());
    FileOutputStream out = new FileOutputStream(directory + "/public.key");
    out.write(DatatypeConverter.printBase64Binary(publicSpec.getEncoded()).getBytes());
    out.close();

    // Store the private key.
    PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
    out = new FileOutputStream(directory + "/private.key");
    out.write(DatatypeConverter.printBase64Binary(privateSpec.getEncoded()).getBytes());
    out.close();
}

From source file:it.nicola_amatucci.util.JsonAndroidLocalIO.java

public static <T> boolean saveData(Context context, String filename, T o, Class<T> obj) {
    String document = null;/*from   www  .j a v  a 2  s  .c  om*/

    try {
        document = Json.json_from_object(o, obj).toString();

        if (document != null) {
            Log.i("TAG", document);
            FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
            fos.write(document.getBytes());
            fos.close();
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;
}

From source file:TripleDES.java

/** Save the specified TripleDES SecretKey to the specified file */
public static void writeKey(SecretKey key, File f)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    // Convert the secret key to an array of bytes like this
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
    DESedeKeySpec keyspec = (DESedeKeySpec) keyfactory.getKeySpec(key, DESedeKeySpec.class);
    byte[] rawkey = keyspec.getKey();

    // Write the raw key to the file
    FileOutputStream out = new FileOutputStream(f);
    out.write(rawkey);
    out.close();/*from  w ww  . ja va  2  s  .  c  o  m*/
}

From source file:com.redsqirl.workflow.utils.FileStream.java

public static void encryptFile(File in, File out) throws Exception {
    //Write the encrypted data to a new file:
    FileOutputStream outStream = new FileOutputStream(out);
    outStream.write(encryptFile(in));
    outStream.close();/* ww  w  . jav a  2  s .com*/
}

From source file:com.redsqirl.workflow.utils.FileStream.java

public static void decryptFile(File in, File out) throws Exception {
    //Write the encrypted data to a new file:
    FileOutputStream outStream = new FileOutputStream(out);
    outStream.write(decryptFile(in));
    outStream.close();// w ww. j  a v  a  2 s  .c  o  m
}

From source file:Main.java

private static void writeWord(FileOutputStream stream, int value) throws IOException {
    byte[] b = new byte[2];
    b[0] = (byte) (value & 0xff);
    b[1] = (byte) (value >> 8 & 0xff);
    stream.write(b);
}

From source file:com.dragome.callbackevictor.serverside.utils.RewritingUtils.java

public static void rewriteClassFile(final File pInput, final ResourceTransformer transformer,
        final File pOutput) throws IOException {

    final byte[] original = toByteArray(pInput);
    byte[] transformed = transformer.transform(original);
    final FileOutputStream os = new FileOutputStream(pOutput);
    os.write(transformed);
    os.close();//  www .  j  a v a 2  s. co m
}

From source file:Main.java

public static boolean writeFile(byte[] buffer, String folder, String fileName) {
    boolean writeSucc = false;

    boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

    String folderPath = "";
    if (sdCardExist) {
        folderPath = Environment.getExternalStorageDirectory() + File.separator + folder + File.separator;
    } else {//from ww w . ja  v  a 2 s  .c om
        writeSucc = false;
    }

    File fileDir = new File(folderPath);
    if (!fileDir.exists()) {
        fileDir.mkdirs();
    }

    File file = new File(folderPath + fileName);
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(file);
        out.write(buffer);
        writeSucc = true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return writeSucc;
}