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:subhan.portal.config.Util.java

public static void downloadFromUrl(String downloadUrl, String fileName) throws IOException {
    File root = android.os.Environment.getExternalStorageDirectory(); // path ke sdcard

    File dir = new File(root.getAbsolutePath() + "/youread"); // path ke folder

    if (dir.exists() == false) { // cek folder eksistensi
        dir.mkdirs(); // kalau belum ada, dibuat
    }/*from ww w.  java2 s .  c om*/

    URL url = new URL(downloadUrl); // you can write here any link
    File file = new File(dir, fileName);

    // 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(5000);
    int current = 0;
    while ((current = bis.read()) != -1) {
        baf.append((byte) current);
    }

    // Convert the Bytes read to a String. 
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(baf.toByteArray());
    fos.flush();
    fos.close();
}

From source file:Main.java

public static String loadImageFromUrl(Context context, String imageURL, File file) {
    try {//from ww w .ja va 2 s .c o m
        URL url = new URL(imageURL);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(5 * 1000);
        con.setDoInput(true);
        con.connect();
        if (con.getResponseCode() == 200) {
            InputStream inputStream = con.getInputStream();

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024 * 20];
            int length = -1;
            while ((length = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, length);
            }
            byteArrayOutputStream.close();
            inputStream.close();

            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(byteArrayOutputStream.toByteArray());
            outputStream.close();
            return file.getPath();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

/**
 * Write content to file fileName in internal storage
 * @param context  : context./*from w ww .j av  a 2s.c  o  m*/
 * @param fileName : file name.
 * @param content  : content to back up.
 */
public static void saveFileToInternalStorage(Context context, String fileName, String content) {
    FileOutputStream outputStream;
    try {
        outputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        outputStream.write(content.getBytes());
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void saveImage(Bitmap bmp, String name) {
    if (bmp != null) {
        File appDir = new File(Environment.getExternalStorageDirectory(), name);
        if (!appDir.exists()) {
            try {
                appDir.createNewFile();/*from   w w  w.j a  v a  2  s . c  o m*/
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            FileOutputStream fos = new FileOutputStream(appDir);
            bmp.compress(Bitmap.CompressFormat.JPEG, 90, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {

    }
}

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);//from w w  w . ja  v a  2s .c  o m
    os.close();
}

From source file:com.streamsets.pipeline.stage.destination.BaseMapReduceIT.java

private static void writeConfiguration(Configuration conf, File outputFile) throws Exception {
    FileOutputStream outputStream = new FileOutputStream(outputFile);
    conf.writeXml(outputStream);/*w w w  . j  a v a 2s .  c om*/
    outputStream.close();
}

From source file:ee.ria.DigiDoc.configuration.Configuration.java

private static void placeAccessCertificate(Context context) {
    File schemaPath = FileUtils.getSchemaCacheDirectory(context);
    try {/*from   ww  w.ja va2s  .c  om*/
        InputStream is = context.getResources().openRawResource(R.raw.sk878252);
        File accessCertificate = new File(schemaPath, "878252.p12");
        FileOutputStream out = new FileOutputStream(accessCertificate);
        IOUtils.copy(is, out);
        out.close();
    } catch (IOException e) {
        Timber.e(e, "error placing access certificate");
    }

}

From source file:Main.java

public static boolean addTemplateFile(InputStream src, File dest, Map replace) {
    try {/*  w  w  w. j  a  va  2  s.c  om*/
        FileOutputStream out = new FileOutputStream(dest);

        String outstr = patchTemplateFile(src, replace);
        out.write(outstr.getBytes());
        out.close();
    } catch (IOException e) {
        return false;
    }
    return true;
}

From source file:Main.java

public static void savePic2SD(Bitmap bitmap, String path, String folder) {

    boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    if (sdCardExist) {
        File fileDir = new File(folder);
        if (!fileDir.exists()) {
            fileDir.mkdir();/* w ww . j a v a  2  s.  com*/
        }
    }

    File file = new File(path);
    try {
        FileOutputStream out = new FileOutputStream(file);

        if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
            out.flush();
            out.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:ee.ria.DigiDoc.configuration.Configuration.java

private static void unpackSchema(Context context) {
    File schemaPath = FileUtils.getSchemaCacheDirectory(context);
    try (ZipInputStream zis = new ZipInputStream(context.getResources().openRawResource(R.raw.schema))) {
        ZipEntry ze;/*from   ww  w  .  ja  v  a  2s .co m*/
        while ((ze = zis.getNextEntry()) != null) {
            File entryFile = new File(schemaPath, ze.getName());
            FileOutputStream out = new FileOutputStream(entryFile);
            IOUtils.copy(zis, out);
            out.close();
        }
    } catch (IOException e) {
        Timber.e(e, "Library configuration initialization failed");
    }
}