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

/**
 * This method serializes an object to an output stream.
 *
 * @param file The output file/* www  . j a va 2  s.c  om*/
 * @param object The object to be serialized
 * @exception IOException IOError
        
 */
public static void serializeObject(File file, Object object) throws IOException {
    FileOutputStream fos = new FileOutputStream(file);
    try {
        ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(fos));
        oos.writeObject(object);
        oos.flush();
    } finally {
        fos.close();
    }
}

From source file:Main.java

public static Uri saveBitmapToSDCard(Bitmap bitmap, String title) {
    File appDir = new File(Environment.getExternalStorageDirectory(), "Gank");
    if (!appDir.exists()) {
        appDir.mkdirs();//from  w  w w .ja va2  s  .com
    }
    String fileName = title.replace("/", "-") + "-girl.jpg";
    File file = new File(appDir, fileName);
    FileOutputStream outputStream;
    try {
        outputStream = new FileOutputStream(file);
        assert bitmap != null;
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream.flush();
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return Uri.fromFile(file);
}

From source file:Main.java

public static void saveBArrToFile(String fileName, byte[] barr) throws IOException {
    File file = new File(fileName);
    file.getParentFile().mkdirs();//from ww  w  . ja v  a  2 s. c om
    File tempFile = new File(fileName + ".tmp");
    FileOutputStream fos = new FileOutputStream(tempFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bos.write(barr, 0, barr.length);
    bos.flush();
    fos.flush();
    bos.close();
    fos.close();
    file.delete();
    tempFile.renameTo(file);
}

From source file:com.amazonaws.encryptionsdk.internal.TestIOUtils.java

public static void generateFile(final String fileName, final long fileSize) throws IOException {
    final FileOutputStream fs = new FileOutputStream(fileName);
    final byte[] fileBytes = new byte[(int) fileSize];
    rng_.nextBytes(fileBytes);/*  ww w.j  a  v a2s. c  o m*/
    fs.write(fileBytes);
    fs.close();
}

From source file:cz.cuni.amis.planning4j.sicstus.AbstractSicstusPlanner.java

private static File createTempFileFromStream(InputStream s) {
    try {/*from  ww w  . j  a va 2  s  .c o  m*/
        File tempFile = File.createTempFile("Sicstus", ".sav");
        FileOutputStream output = new FileOutputStream(tempFile);
        IOUtils.copy(s, output);
        output.close();
        return tempFile;
    } catch (IOException ex) {
        throw new PlanningException("Error preparing save file");
    }

}

From source file:net.firejack.platform.core.utils.SecurityHelper.java

public static void write64(byte[] data, String name) throws IOException {
    FileOutputStream stream = new FileOutputStream(name);
    stream.write(Base64.encode(data));/*from ww  w . j a  v a  2  s .  c  o  m*/
    stream.flush();
    stream.close();
}

From source file:Main.java

public static void unzip(InputStream fin, String targetPath, Context context) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fin));
    try {//from  w  w  w  .j a  va 2 s .c  o m
        ZipEntry zipEntry;
        int count;
        byte[] buffer = new byte[8192];
        while ((zipEntry = zis.getNextEntry()) != null) {
            File file = new File(targetPath, zipEntry.getName());
            File dir = zipEntry.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs()) {
                throw new FileNotFoundException("Failed to get directory: " + dir.getAbsolutePath());
            }
            if (zipEntry.isDirectory()) {
                continue;
            }
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
            Log.d("TEST", "Unzipped " + file.getAbsolutePath());
        }
    } finally {
        zis.close();
    }
}

From source file:Main.java

/**
 * Reads the in_stream and extracts them to out_dir.
 * @param in_stream Input stream corresponding to the zip file.
 * @param out_dir Output directory for the zip file contents.
 * @throws IOException/* www . ja  v  a  2s . c o  m*/
 */
public static void zipExtract(InputStream in_stream, File out_dir) throws IOException {
    if (!out_dir.exists()) {
        if (!out_dir.mkdirs()) {
            throw new IOException("Could not create output directory: " + out_dir.getAbsolutePath());
        }
    }
    ZipInputStream zis = new ZipInputStream(in_stream);
    ZipEntry ze;
    byte[] buffer = new byte[BUFFER_SIZE];
    int count;
    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            File fmd = new File(out_dir.getAbsolutePath() + "/" + ze.getName());
            fmd.mkdirs();
            continue;
        }
        FileOutputStream fout = new FileOutputStream(out_dir.getAbsolutePath() + "/" + ze.getName());
        while ((count = zis.read(buffer)) != -1) {
            fout.write(buffer, 0, count);
        }

        fout.close();
        zis.closeEntry();
    }
    zis.close();
}

From source file:Main.java

public static void writeToExternalFile(String data, String logTag, String fileName) {

    if (!isExternalStorageWritable()) {
        Log.e(logTag, "failed to find external storage");
    } else {/* w w w . j  av  a  2  s .c o  m*/
        File path = Environment.getExternalStorageDirectory();
        File dir = new File(path.getAbsolutePath() + "/SDNController");
        if (!dir.isDirectory()) {
            if (!dir.mkdirs()) {
                Log.e(logTag, "sdn directory can not be created");
                return;
            }
        }

        File file = new File(dir, fileName);
        try {
            FileOutputStream f = new FileOutputStream(file, true);
            PrintWriter pw = new PrintWriter(f);
            pw.println(data);
            pw.flush();
            pw.close();
            f.close();
        } catch (FileNotFoundException e) {
            Log.e(logTag, "can not find indicated file");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(logTag, "failed to write SDNController/result.txt");
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void writeBitmapToDisk(String url, Bitmap bmp, Context ctx, CompressFormat format) {
    FileOutputStream fos;
    String fileName = constructFileName(url);
    try {/*from   www  .j  ava  2s .c  o  m*/
        if (bmp != null) {
            fos = new FileOutputStream(new File(ctx.getCacheDir(), fileName));
            bmp.compress(format, 75, fos);
            fos.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}