Example usage for java.io FileOutputStream flush

List of usage examples for java.io FileOutputStream flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes this output stream and forces any buffered output bytes to be written out.

Usage

From source file:jp.igapyon.diary.v3.util.IgapyonV3Util.java

/**
 * Write html file./*from   w w  w. ja  va2s .  c o m*/
 * 
 * @param strHtml
 * @param file
 * @throws IOException
 */
public static void writeHtmlFile(final String strHtml, final File file) throws IOException {
    final FileOutputStream outStream = new FileOutputStream(file);
    try {
        IOUtils.write(strHtml, outStream, "UTF-8");
        outStream.flush();
    } finally {
        IOUtils.closeQuietly(outStream);
    }
}

From source file:Main.java

public static void saveToFile(Bitmap... bitmaps) {
    int i = 0;/*  w w  w. j a v  a 2s .  c o  m*/
    for (Bitmap bitmap : bitmaps) {
        String path = Environment.getExternalStorageDirectory().getPath();
        File file = new File(path + "/" + (i++) + ".jpg");
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        try {
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.mpower.mintel.android.application.MIntel.java

private static void copyAudioFiles(String[] assetName) {
    for (int i = 0; i < assetName.length; i++) {
        File file = new File(METADATA_PATH, assetName[i]);
        if (!file.exists()) {
            AssetManager mngr = getAppContext().getAssets();
            ByteArrayBuffer baf = new ByteArrayBuffer(2048);
            try {
                InputStream path = mngr.open(assetName[i]);
                BufferedInputStream bis = new BufferedInputStream(path, 1024);
                int current = 0;
                while ((current = bis.read()) != -1) {
                    baf.append((byte) current);
                }/*w  w  w.  j av  a2s .c o  m*/
                byte[] bitmapdata = baf.toByteArray();

                FileOutputStream fos;
                fos = new FileOutputStream(file);
                fos.write(bitmapdata);
                fos.flush();
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static boolean convertBytes2Bitmap(String imageName, byte[] byt) {
    if (byt.length == 0)
        return false;
    boolean success = true;
    Bitmap bmp = BitmapFactory.decodeByteArray(byt, 0, byt.length);
    File file = new File("/sdcard/" + imageName + ".png");

    try {//ww  w  . j a  v  a 2 s  . c  o m
        file.createNewFile();
    } catch (IOException e) {
        success = false;
    }

    FileOutputStream out = null;

    try {
        out = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        success = false;
    }

    bmp.compress(Bitmap.CompressFormat.PNG, 100, out);

    try {
        out.flush();
    } catch (IOException e) {
        success = false;
    }

    try {
        out.close();
    } catch (IOException e) {
    }

    return success;
}

From source file:JDOMUtil.java

/** saves a JDOM Document to a file */
public static void documentToFile(Document doc, String path) {
    try {/*from   ww w  . j a v a 2 s  . c  o  m*/
        FileOutputStream fos = new FileOutputStream(path);
        XMLOutputter xop = new XMLOutputter(Format.getPrettyFormat());
        xop.output(doc, fos);
        fos.flush();
        fos.close();
    } catch (IOException ioe) {
        _log.error("IO Exeception in saving Document to file, filepath = " + path, ioe);
    }
}

From source file:Main.java

private static synchronized void logToFile(String tag, String msg) {
    try {//from www.  j av  a  2s  .co m
        if (LOG_FILE == null) {
            LOG_FILE = new File(LOG_FILE_PATH);
            if (LOG_FILE.exists()) {
                LOG_FILE.delete();
            }
            LOG_FILE.createNewFile();
        }

        FileOutputStream outputStream = new FileOutputStream(LOG_FILE, true);

        StringBuilder builder = new StringBuilder();
        builder.append(tag).append("  :  ").append(msg).append("\n");

        outputStream.write(builder.toString().getBytes());
        outputStream.flush();
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void copyFile(File targetFile, File file) {
    if (targetFile.exists()) {
        return;//w  w  w .  jav a 2s  .  c  o  m
    } else {
        createFile(targetFile, true);
    }
    try {
        FileInputStream is = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(targetFile);
        byte[] buffer = new byte[1024 * 5];
        int len;
        while ((len = is.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        is.close();
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void put(String key, byte[] value) {
    File file = newFile(key);/*from w  ww.  j ava  2 s .c  o  m*/

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(file);
        out.write(value);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

From source file:Main.java

public static boolean copy(File source, File target) {
    if (source == null || target == null || !source.exists() || source.length() < 100) {
        return false;
    }//from  w  w  w . ja v a2  s.  c o m
    try {
        FileOutputStream fos = new FileOutputStream(target);
        FileInputStream fis = new FileInputStream(source);
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = fis.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        fos.flush();
        fos.close();
        fis.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:net.arccotangent.pacchat.filesystem.KeyManager.java

@SuppressWarnings("ResultOfMethodCallIgnored")
public static void saveKeyByIP(String ip_address, PublicKey publicKey) {
    km_log.i("Saving public key for " + ip_address);
    X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(publicKey.getEncoded());
    File pubFile = new File(installationPath + File.separator + ip_address + ".pub");

    km_log.i("Deleting old key if it exists.");
    if (pubFile.exists())
        pubFile.delete();//from  w w w.  j a  v a 2  s. c o  m

    try {
        km_log.i(pubFile.createNewFile() ? "Creation of public key file successful."
                : "Creation of public key file failed!");

        FileOutputStream pubOut = new FileOutputStream(pubFile);
        pubOut.write(Base64.encodeBase64(pubSpec.getEncoded()));
        pubOut.flush();
        pubOut.close();

    } catch (IOException e) {
        km_log.e("Error while saving public key for " + ip_address + "!");
        e.printStackTrace();
    }
}