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

/**
 * (java)write to file//from  w  w  w . j  a v a 2  s  . co m
 * 
 * @param str
 * @param path
 */
public static void writeFile(String str, File file) {
    FileOutputStream out;
    try {
        file.createNewFile();

        out = new FileOutputStream(file, false);
        String infoToWrite = str;
        out.write(infoToWrite.getBytes());
        out.close();

    } catch (IOException e) {
        Log.e("", "write error!");
    }
}

From source file:Main.java

public static Bitmap getCompressBitmap(Bitmap bitmap, File file, int quality, int fileSize) {
    Bitmap bmp = null;//from w  ww .jav a2 s.com
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    boolean result = bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos);
    LOG("getCompressBitmap result: " + result);
    try {
        if (file != null) {
            if (result) {
                byte[] bt = bos.toByteArray();

                FileOutputStream fos = new FileOutputStream(file);
                fos.write(bt);
                fos.close();

                LOG("file.length(): " + file.length());

                if (file.length() > fileSize) {
                    bmp = getCompressBitmap(bmp, file, (int) (quality * 0.8), fileSize);
                } else {
                    bmp = BitmapFactory.decodeFile(file.getPath());
                }
            }

        } else {
            bmp = BitmapFactory.decodeByteArray(bos.toByteArray(), 0, bos.size());
        }
        bos.close();
    } catch (Exception e) {
        LOG("getCompressBitmap result: e" + e.toString());
        e.printStackTrace();
        return null;
    }
    return bmp;
}

From source file:Main.java

public static void imgCacheWrite(Context context, String cacheImgFileName, String imgBase64Str) {
    File cacheImgFile = new File(context.getFilesDir() + "/" + cacheImgFileName);
    if (cacheImgFile.exists()) {
        cacheImgFile.delete();/*from   w ww  .jav  a  2  s  . c o m*/
    }

    FileOutputStream fos;
    try {
        fos = context.openFileOutput(cacheImgFileName, Context.MODE_PRIVATE);
        fos.write(imgBase64Str.getBytes("utf-8"));
        fos.flush();
        fos.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:Main.java

public static File downloadFile(String urlstr, File saveFile) {
    try {/*w w  w.j  av a 2s  . c om*/
        URL url = new URL(urlstr);// cause speed low.
        URLConnection con = url.openConnection();
        con.setDoInput(true);
        con.connect();
        InputStream ins = con.getInputStream();
        final int bufsize = 102400;

        byte[] buffer = new byte[bufsize];
        int len = -1;
        FileOutputStream bos = new FileOutputStream(saveFile);
        while ((len = ins.read(buffer)) != -1) {
            bos.write(buffer, 0, len);

        }
        ins.close();
        bos.close();
        return saveFile;
    } catch (Error e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.aqnote.shared.cryptology.cert.util.X509CertFileUtil.java

public static void writeCert(String b64cert, String certFileName) throws IOException {

    if (StringUtils.isBlank(b64cert) || StringUtils.isBlank(certFileName)) {
        return;//from   ww w  .  ja va  2 s .c  om
    }
    FileOutputStream fos1 = new FileOutputStream(certFileName);
    fos1.write(b64cert.getBytes());
    fos1.flush();
    fos1.close();
}

From source file:Main.java

private static boolean tryToSaveBitmap(Bitmap bitmap, String savePath) throws IOException {
    FileOutputStream output = new FileOutputStream(savePath);
    try {/*  ww w .j av  a 2  s .c om*/
        return bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
    } finally {
        try {
            output.flush();
            output.close();
        } catch (Exception e) {
            // ignore...
        }
    }
}

From source file:Main.java

public static void writeToFile(File file, byte[] data) throws IOException {
    FileOutputStream fou = null;
    try {//from   w  w w  .  j a v a 2 s .  co  m
        fou = new FileOutputStream(file);
        fou.write(data);
    } finally {
        if (fou != null) {
            try {
                fou.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:Main.java

public static boolean putFileContent(File file, InputStream inputStream) throws IOException {
    if (!file.exists() && !file.createNewFile()) {
        return false;
    }//from www. j  av  a2 s.c  o m

    byte[] buffer = new byte[1024];
    FileOutputStream fileOutputStream = new FileOutputStream(file);

    for (int len; (len = inputStream.read(buffer)) != -1;) {
        fileOutputStream.write(buffer, 0, len);
    }

    inputStream.close();
    fileOutputStream.close();
    return true;
}

From source file:Main.java

public static Boolean writeToSDFile(String directory, String file_name, String text) {

    // Find the root of the external storage.
    // See//from w  w  w.  ja  v a  2 s. c o m
    // http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

    File root = Environment.getExternalStorageDirectory();

    // See
    // http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder

    File dir = new File(root.getAbsolutePath() + "/" + directory);
    dir.mkdirs();
    File file = new File(dir, file_name);

    try {
        FileOutputStream f = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(f);
        pw.println(text);
        pw.flush();
        pw.close();
        f.close();
        // Log.v(TAG, "file written to sd card");
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        // Log.i(TAG, "******* File not found. Did you" +
        // " add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:com.camel.trainreserve.TicketReserver.java

private static void getCaptchaImg() {
    String url = "https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=passenger&rand=randp&0.20558934959469144";
    ByteArrayOutputStream outStream = JDKHttpsClient.doGetImg(url, cookieStr);
    if (outStream.size() > 0) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        File imageFile = new File("G://12306OCR-2//" + (new Date()).getTime() + ".png");
        try {// www.  j a  v  a 2 s . c o  m
            FileOutputStream fos = new FileOutputStream(imageFile);
            byte[] bytes = outStream.toByteArray();
            fos.write(bytes);
            fos.flush();
            fos.close();
            outStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        System.out.println(Thread.currentThread().getName() + " return empty");
    }
}