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

public static void Stream2File(InputStream is, File file) {
    byte[] b = new byte[1024];
    int len;//  w ww .  ja  v a  2  s  .c o  m
    FileOutputStream os = null;
    try {
        os = new FileOutputStream(file);
        while ((len = is.read(b)) != -1) {
            os.write(b, 0, len);
            os.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeIO(is, os);
    }
}

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 w w .  j ava2  s .  c  om*/
    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:com.pentaho.repository.importexport.PDIImportUtil.java

/**
 * @return instance of {@link Document}, if xml is loaded successfully null in case any error occurred during loading
 *//*  ww  w .  j a v  a 2s .  c om*/
public static Document loadXMLFrom(InputStream is) {
    DocumentBuilderFactory factory;
    try {
        factory = XMLParserFactoryProducer.createSecureDocBuilderFactory();
    } catch (ParserConfigurationException e) {
        log.logError(e.getLocalizedMessage());
        factory = DocumentBuilderFactory.newInstance();
    }
    DocumentBuilder builder = null;
    Document doc = null;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        // ignore
    }
    try {
        File file = File.createTempFile("tempFile", "temp");
        file.deleteOnExit();
        FileOutputStream fous = new FileOutputStream(file);
        IOUtils.copy(is, fous);
        fous.flush();
        fous.close();
        doc = builder.parse(file);
    } catch (IOException | SAXException e) {
        log.logError(e.getLocalizedMessage());
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            // nothing to do here
        }
    }
    return doc;
}

From source file:Main.java

public static void saveObj(Object obj, String fileName) throws IOException {
    File f = new File(fileName + ".tmp");
    f.getParentFile().mkdirs();//from   ww w  . ja v  a2  s  .c o 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 saveBArrToFile(String fileName, byte[] barr) throws IOException {
    File file = new File(fileName);
    file.getParentFile().mkdirs();/*from w  ww.ja  v a 2 s.  co m*/
    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:Main.java

public static void writeFile(InputStream in, File file) throws IOException {
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();/*from w w  w  .j  a v  a  2  s  .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 void writeFile(InputStream in, File file) throws IOException {
    if (!file.getParentFile().exists())
        file.getParentFile().mkdirs();//from ww  w  . j a v a2 s  .c  o m

    if (file != null && file.exists())
        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 void createFileFormInputStream(InputStream is, String path) {
    try {/*  ww  w.j  a v a  2 s.  co  m*/
        FileOutputStream fos = new FileOutputStream(path);
        byte[] buf = new byte[1376];
        while (is.read(buf) > 0) {
            fos.write(buf, 0, buf.length);
        }
        is.close();
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean saveBitmapToLocal(String path, String fileName, Bitmap b) {
    if (b == null) {
        return false;
    }/*from   w w  w .  ja  v  a 2 s .  com*/

    boolean result = false;
    String storageState = Environment.getExternalStorageState();
    if (storageState.equals(Environment.MEDIA_MOUNTED)) {

        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(path + fileName);
            b.compress(CompressFormat.JPEG, 100, fos);
            fos.flush();
            result = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
    return result;
}

From source file:Main.java

public static void saveBitmap(String dirpath, String filename, Bitmap bitmap, boolean isDelete) {
    File dir = new File(dirpath);
    if (!dir.exists()) {
        dir.mkdirs();//from  w  w  w .ja  v  a2 s.  c o  m
    }

    File file = new File(dirpath, filename);
    if (isDelete) {
        if (file.exists()) {
            file.delete();
        }
    }

    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(file);
        if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {
            out.flush();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}