Example usage for java.io FileOutputStream close

List of usage examples for java.io FileOutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this file output stream and releases any system resources associated with this stream.

Usage

From source file:be.vds.jtbdive.core.utils.FileUtilities.java

public static void replaceAllInFile(File file, String[] oldValues, String[] newValues) throws IOException {
    byte[] b = new byte[(int) file.length()];
    FileInputStream is = new FileInputStream(file);
    is.read(b);/*  w ww .  j  a va2  s  . com*/
    is.close();
    String s = new String(b);

    for (int i = 0; i < oldValues.length; i++) {
        s = s.replaceAll(oldValues[i], newValues[i]);
    }

    FileOutputStream os = new FileOutputStream(file);
    os.write(s.getBytes());
    os.flush();
    os.close();

}

From source file:Main.java

public static boolean storeImage(Context context, Bitmap bmp, boolean isRotate) {

    // use the current data&time for image file name
    String takenTime_YYMMDD_HHMMSS = new SimpleDateFormat(DATA_FORMAT).format(new Date());

    // saved bitmap: full path
    String path = PIC_ROOT_PATH + takenTime_YYMMDD_HHMMSS;
    File f = new File(path);

    if (f != null && !f.getParentFile().exists()) {
        f.getParentFile().mkdirs();//from w w w .j  ava  2s  . c om
    }

    if (isRotate) {
        Matrix matrix = new Matrix();
        matrix.reset();
        matrix.postRotate(90);
        bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
    }

    try {
        FileOutputStream out = new FileOutputStream(f);
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    return updateGallery(context, bmp, takenTime_YYMMDD_HHMMSS);
}

From source file:Main.java

public static void saveObj(Object obj, String fileName) throws IOException {
    File f = new File(fileName + ".tmp");
    f.getParentFile().mkdirs();// ww  w .  j  ava  2  s  . co  m
    FileOutputStream fos = new FileOutputStream(f);
    ObjectOutputStream out = new ObjectOutputStream(fos);
    out.writeObject(obj);
    out.flush();
    out.close();
    fos.flush();
    fos.close();
    File file = new File(fileName);
    file.delete();
    f.renameTo(file);
}

From source file:Main.java

public static void inputToOutput(FileOutputStream outputStream, InputStream inputStream) throws IOException {
    byte[] buffer = new byte[1024];
    int len;//from   www .  j a v  a 2  s .  c o  m
    while ((len = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, len);
    }
    outputStream.close();
    inputStream.close();
}

From source file:com.bahmanm.karun.Utils.java

/**
 * Copies srcPath to destPath.//from w ww  . j  a  va 2s.  co  m
 * 
 * @param srcPath Path to source file
 * @param destPath Path to destination file
 * @throws FileNotFoundException
 * @throws IOException 
 */
public synchronized static void copyFile(String srcPath, String destPath)
        throws FileNotFoundException, IOException {
    FileInputStream in = new FileInputStream(srcPath);
    FileOutputStream out = new FileOutputStream(destPath);
    IOUtils.copy(in, out);
    in.close();
    out.close();
}

From source file:com.hernandez.rey.crypto.TripleDES.java

/**
 * Save the specified TripleDES SecretKey to the specified file
 * /*from ww  w .  j  a  v a 2 s .  co m*/
 * @param key the key to write
 * @param keyFile
 * @throws IOException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 */
public static void writeKey(final SecretKey key, final File keyFile)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    // Convert the secret key to an array of bytes like this
    final SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
    final DESedeKeySpec keyspec = (DESedeKeySpec) keyfactory.getKeySpec(key, DESedeKeySpec.class);
    final byte[] rawkey = keyspec.getKey();

    final byte[] encodedKey = Base64.encodeBase64(rawkey);

    // Write the raw key to the file
    FileOutputStream out = new FileOutputStream(keyFile);
    out.write(encodedKey);
    out.close();

    out = new FileOutputStream(new File(keyFile.toString().concat("-raw")));
    out.write(rawkey);
    out.close();
}

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

/**
 * Saves the key pair to the disk./*from   w  w  w. j a v  a 2 s . c om*/
 * 
 * @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:Main.java

private static void copyResourceFile(Context context, int rid, String targetFile) throws IOException {
    InputStream fin = context.getResources().openRawResource(rid);
    FileOutputStream fos = new FileOutputStream(targetFile);

    int length;/*from w  w  w  .  ja v  a  2 s. co  m*/
    byte[] buffer = new byte[1024 * 32];
    while ((length = fin.read(buffer)) != -1) {
        fos.write(buffer, 0, length);
    }
    fin.close();
    fos.close();
}

From source file:Main.java

public static void writeFile(InputStream in, File file) throws IOException {
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();// ww  w .  j a v  a2s  .c o  m
    } else {
        file.delete();
    }

    FileOutputStream out = new FileOutputStream(file);
    byte[] buffer = new byte[1024 * 128];
    int len = -1;
    while ((len = in.read(buffer)) != -1) {
        out.write(buffer, 0, len);
    }
    out.flush();
    out.close();
    in.close();
}

From source file:Main.java

public static boolean writeToExternalStoragePublic(String filename, String fileContent) {
    boolean saved = false;
    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
    if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) {
        try {//from w  w w. j av a2s . c o  m
            boolean exists = (new File(path)).exists();
            if (!exists) {
                new File(path).mkdirs();
            }

            //Remote legacy file
            File file = new File(path + filename);
            file.delete();

            // Open output stream
            FileOutputStream fOut = new FileOutputStream(path + filename, true);
            fOut.write(fileContent.getBytes());
            // Close output stream
            fOut.flush();
            fOut.close();

            saved = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return saved;
}